【发布时间】:2018-02-04 20:05:11
【问题描述】:
问题:
正如您在图像上看到的,text_field 和 can_frame(画布内的框架)没有填充紫色空白。
我的应用程序是如何构建的:
我的根窗口中有right_frame(紫色)和left_frame(粉色)。我在right_frame 中放置了一个canvas,在其中放置了一个我称之为can_frame 的框架。
现在在那个 can_frame 我放了两个 text_fields 。
问题是,如何让can_frame 和text_fields(在can_frame 中)填充紫色空间。
我制作的一个新的简单应用程序的完整代码:
class Analyse_Window:
def __init__(self, root):
root.grid_rowconfigure (0, weight=1)
root.grid_columnconfigure(1, weight=1)
self.left_frame = Frame( root, bg='pink', height=580, width=450)
self.right_frame = Frame(root, bg='light blue', height=580, width=450)
self.left_frame.grid( row=0, column=0, sticky='nsew')
self.right_frame.grid(row=0, column=1, sticky='nsew')
self.right_frame.grid_columnconfigure(0, weight=1)
self.right_frame.grid_rowconfigure(0, weight=1)
# Create and setup canvas, scrollbar, and right frame with text fields.
self.canvas = Canvas(self.right_frame, borderwidth=10, bg='purple')
self.can_frame = Frame(self.canvas, bg='yellow')
self.scrollbar = Scrollbar(self.right_frame, orient='vertical', command=self.canvas.yview)
self.scrollbar2 = Scrollbar(self.right_frame, orient='horizontal',command=self.canvas.xview, width=25)
self.canvas.configure(yscrollcommand=self.scrollbar.set, xscrollcommand=self.scrollbar2.set)
# Grid canvas, frame, and scrollbars
self.scrollbar.grid(row=0, column=1, sticky='ns')
self.scrollbar2.grid(row=1, sticky='ew')
self.canvas.grid( row=0, column=0, sticky='nsew')
self.can_frame.grid(row=0, column=0, sticky='nsew')
self.canvas.grid_columnconfigure((0,1), weight=1)
self.canvas.grid_rowconfigure((0,1), weight=1)
self.canvas.create_window((4,4), window=self.can_frame, anchor='sw', tags="self.frame")
self.can_frame.bind( "<Configure>", self.onFrameConfigure)
self.canvas.bind_all("<MouseWheel>", self._on_mousewheel)
self.canvas.bind('<Configure>', self.foo)
# Create text fields and put them in the canvas frame
inp = Text(self.can_frame, width=40, relief='groove')
inp.insert(END, 'this is a test')
inp.configure(state=tkinter.DISABLED)
inp.grid(row=0, column=0, sticky='nsew')
inp2 = Text(self.can_frame, width=20, relief='groove')
inp2.grid(row=2, column=0)
def foo(self, event):
w,h = event.width-100, event.height-100
self.canvas.config(width=w, height=h)
def onFrameConfigure(self, event):
# update scrollregion after starting 'mainloop'
# when all widgets are in canvas
self.canvas.configure(scrollregion=self.canvas.bbox('all'))
def _on_mousewheel(self, event):
self.canvas.yview_scroll(int(-1*(event.delta/120)), 'units')
if __name__ == '__main__':
root = Tk()
b = Analyse_Window(root)
root.mainloop()
您可以尝试将代码复制粘贴到您自己的编辑器中并自己尝试吗?
【问题讨论】:
-
@BryanOakley 我希望你现在能帮助我,我刚刚更新了所有内容,我什至将整个脚本复制到了一个新文档中,并删除了所有不必要的东西。