如何使tkinter框架充满整个画布并与之一起扩展?

时间:2019-09-01 07:23:16

标签: python tkinter tkinter-canvas tkinter-layout

我想制作一个可滚动的框架,以填充应用程序窗口中的整个单元格。调整窗口大小时,框架中的小部件应保持居中。由于框架不可滚动,因此我使用了画布并将框架放置在其窗口中。滚动区域与框架相关联。但是,我无法使框架扩展并填充整个画布区域。

我试图在画布上为具有整个画布宽度的框架创建一个窗口,并将该窗口的宽度变化与画布配置事件相关联。但是我得到了有线结果。框架仅占据窗口的右侧。当我展开窗口时,它会向左移动。我将画布涂成黄色,将框架涂成绿色,以使事物可见。 感谢您的帮助!

driver.execute('mobile: touchAndHold', {duration: 1.5,x:10,y:10,element: ele.value.ELEMENT }, function(err) {
console.log(err)
resolve();
});

1 个答案:

答案 0 :(得分:1)

谢谢,stovfl! 工作代码,以防有人需要


import tkinter as tk


class FrameWithScrollBar(tk.Frame):

    def __init__(self, parent, *args, **kwargs):
        super().__init__(parent, *args, **kwargs)
        self.canvas = tk.Canvas(self, bg='yellow')
        self.frame = tk.Frame(self.canvas, bg='green')
        self.scrollbar = tk.Scrollbar(self, orient='vertical',
                                command=self.canvas.yview)
        self.canvas.configure(yscrollcommand=self.scrollbar.set)
        self.canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
        self.scrollbar.pack(side=tk.RIGHT, fill=tk.BOTH)
        self.frame.pack(fill=tk.BOTH, expand=True)
        self._frame_id = self.canvas.create_window(
                                 self.canvas.winfo_width(), 0,
                                 anchor='nw',
                                 window=self.frame)
        self.frame.bind('<Configure>', self.onFrameConfigure)
        self.canvas.bind('<Configure>', self.onCanvasConfigure)

    def onFrameConfigure(self, event):       
        self.canvas.configure(scrollregion=self.frame.bbox('all'))

    def onCanvasConfigure(self, event):
        width = event.width
        self.canvas.itemconfigure(self._frame_id, width=self.canvas.winfo_width())


if __name__ == '__main__':

    root = tk.Tk()
    fws = FrameWithScrollBar(root)
    buttons = list()
    for i in range(5):
        for j in range(25):
            button = tk.Button(fws.frame, text='Button ' + str(i) + ','+str(j))
            button.grid(row=j, column=i, sticky='wesn')
            tk.Grid.columnconfigure(fws.frame, i, weight=1)
    fws.pack(expand=True, fill=tk.BOTH)
    root.mainloop()