根据Stack Overflow中的一些答案,我通过创建画布并将框架嵌入到画布中来创建滚动条,但是这样做之后,我只能在滚动条中单击 arrow 来滚动窗口,但是无论我缩小窗口还是在框架中创建许多小部件,该栏都不会显示。
class Basic_frame:
def __init__(self):
root.unbind('<Configure>')
self.frame_canvas = Canvas(
root, width=i_window_width, height=i_window_height, bg='red')
self.window_frame = Frame(
self.frame_canvas)
self.xscrollbar = Scrollbar(
self.frame_canvas, orient='horizontal', command=self.frame_canvas.xview)
self.yscrollbar = Scrollbar(
self.frame_canvas, orient='vertical', command=self.frame_canvas.yview)
self.frame_canvas.configure(yscrollcommand=self.yscrollbar.set)
self.frame_canvas.configure(xscrollcommand=self.xscrollbar.set)
self.frame_canvas.xview_moveto(0)
self.frame_canvas.yview_moveto(0)
self.yscrollbar.pack(side='right', fill='y')
self.xscrollbar.pack(side='bottom', fill='x')
self.frame_canvas.create_window(
(0, 0), window=self.window_frame, anchor='nw')
# self.window_frame.bind('<Configure>', self.scroll_function)
self.frame_canvas.pack(side=LEFT, fill=BOTH, expand=TRUE)
self.frame_canvas.bind('<Configure>', self.resize)
self.window_frame.bind('<Configure>', self.frame_size)
def frame_size(self, event):
size = (root.winfo_reqwidth(), root.winfo_reqheight())
self.frame_canvas.config(scrollregion=(0, 0, 100, 100))
# self.frame_canvas.configure(scrollregion=self.window_frame.bbox('all'))
self.frame_canvas.config(width=event.width, height=event.height)