2013-11-05 29 views
1

使用Tkinter for 2.7.5我想在下面的代碼中添加一個滾動條,以便可以查看所有的按鈕。如何添加一個滾動條到下面的代碼? (Tkinter爲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() 
+2

查看這些 - 1. http://www.tutorialspoint.com/python/tk_scrollbar.htm&2. http://effbot.org/tkinterbook /scrollbar.htm 並自己嘗試! :) –

+2

我也建議使用['for' loop](http://docs.python.org/2/tutorial/controlflow.html?highlight=loop#for-statements)來創建所有這些按鈕。 – martineau

+0

我如何在for循環中執行它?請舉個例子? – KingMak

回答

0

你可以把按鈕放在畫布由於畫布組件支持滾動條界面。但是,您必須使用其窗口座標將按鈕顯式放置在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() 
+0

謝謝菲弗,這有很大的幫助 – KingMak

相關問題