【发布时间】:2018-06-03 10:55:55
【问题描述】:
我(在帮助下)创建了一个带有滚动条的 Frame 类。似乎效果很好,但我希望按钮的大小适合框架的大小。现在,如果您看,它们都聚集在左侧。我浏览了很多例子,这似乎很常见。下面包含一些易于运行的代码,因此您可以看到问题。下面附上问题的图片。
谢谢。
from tkinter import *
class ScrolledFrame(Frame):
def __init__(self, top, *args, **kwargs):
Frame.__init__(self, top, *args, **kwargs)
hscrollbar = Scrollbar(self, orient=HORIZONTAL)
hscrollbar.grid(row=1, column=0, sticky=E+W)
vscrollbar = Scrollbar(self, orient=VERTICAL)
vscrollbar.grid(row=0, column=1, sticky=N+S)
self.canvas = Canvas(self, xscrollcommand=hscrollbar.set, yscrollcommand=vscrollbar.set)
self.canvas.grid(row=0, column=0, sticky=N+S+E+W)
hscrollbar.config(command = self.canvas.xview)
vscrollbar.config(command = self.canvas.yview)
# Make the canvas expandable
self.grid_rowconfigure(0, weight=1)
self.grid_columnconfigure(0, weight=1)
# Create the canvas contents
self.frame = Frame(self.canvas)
self.frame.rowconfigure(1, weight=1)
self.frame.columnconfigure(1, weight=1)
self.canvas.create_window(0, 0, window=self.frame, anchor=N+W)
self.canvas.config(scrollregion=self.canvas.bbox('all'))
self.frame.bind('<Configure>', self.frame_changed)
def frame_changed(self, event):
self.frame.update_idletasks()
self.canvas.config(scrollregion=self.canvas.bbox('all'))
root = Tk()
frame = ScrolledFrame(root)
frame.grid(row=0, column=0, sticky=N+E+W+S)
root.rowconfigure(0, weight=1)
root.columnconfigure(0, weight=1)
for i in range(20):
button = Button(frame.frame, text='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx %d' % i)
button.pack(fill=BOTH, side=TOP, expand=True)
root.mainloop()
【问题讨论】:
标签: python tkinter positioning