1

使用 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()
4

1 回答 1

0

您可以将按钮放在 Canvas 中,因为 Canvas 小部件支持 Scrollbar 界面。但是,您必须使用其窗口坐标而不是使用 pack 或 grid 将按钮显式放置在 Canvas 中。这是一个示例(带有 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()
于 2013-11-08T15:35:45.533 回答