Tkinter, canvas unable to scroll

╄→尐↘猪︶ㄣ 提交于 2019-12-13 04:02:20

问题


I'm trying to make a Tkinter window class that contains a canvas with a scrollbar based on the tkinter Toplevel class. When I run my code I don't receive any errors but the scrollbar in the window is disabled. The Frame or canvas that has the information wont stretch with the window when I stretch it manually after the program is running. Here is the bugged code:

class new_window(Toplevel):

    def __init__(self,master):
        Toplevel.__init__(self,master)


        self.grid_rowconfigure(0, weight=1)
        self.grid_columnconfigure(0, weight=1)

        s = Scrollbar(self, orient = VERTICAL)
        s.grid(row = 0, column = 1, sticky = NS)
        self.can = Canvas(self,  yscrollcommand=s.set)
        self.can.grid(row = 0, column = 0, sticky = N+S+E+W)
        self.win = Frame(self.can)
        self.can.create_window(0,0, window = self.win, anchor = NW)
        s.config(command = self.can.yview)

        size = (self.win.winfo_reqwidth(), self.win.winfo_reqheight())
        self.can.config(scrollregion="0 0 %s %s" % size)

        self.win.update_idletasks()
        self.ca.configure(scrollregion = (1,1,win.winfo_width(),win.winfo_height()))


    def create(self):
        for i in range (100):
            i = Label(self.win, text = str(i))
            i.grid()


root = Tk()

win = new_window(root)
win.create()
root.mainloop()

It was working fine before I decided to implement classes:

from Tkinter import *

root = Tk()

window = Toplevel()
window.grid_rowconfigure(0, weight=1)
window.grid_columnconfigure(0, weight=1)

s = Scrollbar(window, orient = VERTICAL)
s.grid(row = 6, column = 1, sticky = NS)
can = Canvas(window, width = 1600, height = 700, yscrollcommand=s.set)
can.grid(row = 6, column = 0, sticky = NSEW)
win = Frame(can)
can.create_window(0,0, window = win, anchor = NW)
s.config(command = can.yview)

for i in range(100):
    lbl = Label(win, text = str(i))
    lbl.grid()

win.update_idletasks()
can.configure(scrollregion = (1,1,win.winfo_width(),win.winfo_height()))

root.mainloop()

Im not sure where I went wrong in the transition, any help would be greatly appreciated.


回答1:


I think the issue is coming from here:

self.win.update_idletasks()
self.ca.configure(scrollregion = (1,1,win.winfo_width(),win.winfo_height()))

This is inside the initialization function, when it should be updating after the create function is called. There's still probably a more efficient way to structure this, but this should work in the meantime:

from Tkinter import *

class new_window(Toplevel):

    def __init__(self,master):
        Toplevel.__init__(self,master)

        self.grid_rowconfigure(0, weight=1)
        self.grid_columnconfigure(0, weight=1)

        s = Scrollbar(self, orient = VERTICAL)
        s.grid(row = 0, column = 1, sticky = NS)
        self.can = Canvas(self,  yscrollcommand=s.set)
        self.can.grid(row = 0, column = 0, sticky = N+S+E+W)
        self.win = Frame(self.can)
        self.can.create_window(0,0, window = self.win, anchor = NW)
        s.config(command = self.can.yview)

        size = (self.win.winfo_reqwidth(), self.win.winfo_reqheight())
        self.can.config(scrollregion="0 0 %s %s" % size)

    def create(self):
        for i in range (100):
            i = Label(self.win, text = str(i))
            i.grid()
        self.win.update_idletasks()
        self.can.configure(scrollregion = (1,1,self.win.winfo_width(),self.win.winfo_height()))

root = Tk()

win = new_window(root)
win.create()
root.mainloop()


来源:https://stackoverflow.com/questions/21609141/tkinter-canvas-unable-to-scroll

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!