使用Tkinter for 2.7.5我想在下面的代码中添加一个滚动条,以便可以查看所有按钮。
谢谢。
代码:
import Tkinter, tkMessageBox root = Tkinter.Tk() def centerRoot(w = 240, h = 498): ws = root.winfo_screenwidth() hs = root.winfo_screenheight() x = (ws/2) - (w/2) y = (hs/2) - (h/2) root.geometry('%dx%d+%d+%d' % (w, h, x, y)) Tkinter.Button(root, height=2, width=10, text='Text', borderwidth=10).grid(row=0,column=0) Tkinter.Button(root, height=2, width=10, text='Text', borderwidth=10).grid(row=0,column=1) Tkinter.Button(root, height=2, width=10, text='Text', borderwidth=10).grid(row=1,column=0) Tkinter.Button(root, height=2, width=10, text='Text', borderwidth=10).grid(row=1,column=1) Tkinter.Button(root, height=2, width=10, text='Text', borderwidth=10).grid(row=2,column=0) Tkinter.Button(root, height=2, width=10, text='Text', borderwidth=10).grid(row=2,column=1) Tkinter.Button(root, height=2, width=10, text='Text', borderwidth=10).grid(row=3,column=0) Tkinter.Button(root, height=2, width=10, text='Text', borderwidth=10).grid(row=3,column=1) Tkinter.Button(root, height=2, width=10, text='Text', borderwidth=10).grid(row=4,column=0) Tkinter.Button(root, height=2, width=10, text='Text', borderwidth=10).grid(row=4,column=1) Tkinter.Button(root, height=2, width=10, text='Text', borderwidth=10).grid(row=5,column=0) Tkinter.Button(root, height=2, width=10, text='Text', borderwidth=10).grid(row=5,column=1) Tkinter.Button(root, height=2, width=10, text='Text', borderwidth=10).grid(row=6,column=0) Tkinter.Button(root, height=2, width=10, text='Text', borderwidth=10).grid(row=6,column=1) Tkinter.Button(root, height=2, width=10, text='Text', borderwidth=10).grid(row=7,column=0) Tkinter.Button(root, height=2, width=10, text='Text', borderwidth=10).grid(row=7,column=1) Tkinter.Button(root, height=2, width=10, text='Text', borderwidth=10).grid(row=8,column=0) Tkinter.Button(root, height=2, width=10, text='Text', borderwidth=10).grid(row=8,column=1) Tkinter.Button(root, height=2, width=10, text='Text', borderwidth=10).grid(row=9,column=0) Tkinter.Button(root, height=2, width=10, text='Text', borderwidth=10).grid(row=9,column=1) Tkinter.Button(root, height=2, width=10, text='Text', borderwidth=10).grid(row=10,column=0) Tkinter.Button(root, height=2, width=10, text='Text', borderwidth=10).grid(row=10,column=1) Tkinter.Button(root, height=2, width=10, text='Text', borderwidth=10).grid(row=11,column=0) Tkinter.Button(root, height=2, width=10, text='Text', borderwidth=10).grid(row=11,column=1) Tkinter.Button(root, height=2, width=10, text='Text', borderwidth=10).grid(row=12,column=0) Tkinter.Button(root, height=2, width=10, text='Text', borderwidth=10).grid(row=12,column=1) Tkinter.Button(root, height=2, width=10, text='Text', borderwidth=10).grid(row=13,column=0) Tkinter.Button(root, height=2, width=10, text='Text', borderwidth=10).grid(row=13,column=1) Tkinter.Button(root, height=2, width=10, text='Text', borderwidth=10).grid(row=14,column=0) Tkinter.Button(root, height=2, width=10, text='Text', borderwidth=10).grid(row=14,column=1) Tkinter.Button(root, height=2, width=10, text='Text', borderwidth=10).grid(row=15,column=0) Tkinter.Button(root, height=2, width=10, text='Text', borderwidth=10).grid(row=15,column=1) centerRoot() root.title('Title Here') root.mainloop()
答案 0 :(得分:0)
您可以将按钮放在Canvas中,因为Canvas小部件支持Scrollbar界面。但是,您必须使用窗口坐标明确地将按钮放在“画布”中,而不是使用包或网格。这是一个例子(带有for循环:):
import Tkinter
class Application(Tkinter.Frame):
def __init__(self, master, width, height):
Tkinter.Frame.__init__(self, master)
self.master.minsize(width=width, height=height)
self.master.config()
self.pack()
self.main_frame = Tkinter.Frame(self.master)
self.button_scroll_bar = Tkinter.Scrollbar(
self.main_frame,
orient='vertical')
self.button_canvas = Tkinter.Canvas(
self.main_frame,
yscrollcommand=self.button_scroll_bar.set,
relief='flat',
borderwidth=0)
self.button_canvas.bind('<MouseWheel>', self._on_mousewheel)
self.button_scroll_bar.config(command=self.button_canvas.yview)
self.button_scroll_bar.pack(side='right', fill='y')
self.button_canvas.pack(fill='both', expand=True)
for i in range(0, 15):
for j in range(0, 2):
b = Tkinter.Button(
self.button_canvas,
text='Text')
# note we are separating the buttons 80 units horizontally
# and 30 units vertically...I just chose these numbers
# since they looked reasonable.
self.button_canvas.create_window(
(80 * j),
(30 * i),
anchor='nw',
window=b
)
# here we need to update the scrollregion, notice the same x, y numbers
# multiplied times the number of buttons we placed in the canvas
self.button_canvas.config(scrollregion=(0, 0, 80*2, 30*15))
self.main_frame.pack(fill='both', expand=True)
def _on_mousewheel(self, event):
self.button_canvas.yview_scroll(-event.delta, "units")
root = Tkinter.Tk()
root.title('Title Here')
w = 512
h = 256
app = Application(root, width=w, height=h)
app.mainloop()