2016-02-25 34 views
-5

我想在python 3中製作一個簡單的測驗程序,但我無法找到如何使按鈕走完我想要的全部寬度。我正在使用python 3和TKinter模塊來創建窗口和所有按鈕。如何在Tkinter中設置按鈕寬度(Python 3)

from tkinter import * 

root = Tk() 

que_labl = Label(root, text='Question') 
choice1 = Button(root, text='Choice one') 
choice2 = Button(root, text='Choice one plus one') 
choice3 = Button(root, text='Choice 3') 
choice4 = Button(root, text='Choice eight divided by 2') 

que_labl.grid(row=0, columnspan=2) 
choice1.grid(row=2, column=0, sticky=W) 
choice2.grid(row=2, column=1, sticky=W) 
choice3.grid(row=3, column=0, sticky=W) 
choice4.grid(row=3, column=1, sticky=W) 

root.mainloop() 

的代碼使一個窗口是這樣的:

enter image description here

+3

簡單的谷歌搜索會給你正確的答案 – IsaacDj

+2

問問你自己「sticky = W'做什麼? –

回答

1

使用Grid.rowconfigure()Grid.columnconfigure()sticky=E+W

from Tkinter import * 

root = Tk() 
#Configure line 0 and 1 
Grid.rowconfigure(root, 0, weight=1) 
Grid.rowconfigure(root, 1, weight=1) 

#Configure column 0 and 1 
Grid.columnconfigure(root, 0, weight=1) 
Grid.columnconfigure(root, 1, weight=1) 

que_labl = Label(root, text='Question') 
choice1 = Button(root, text='Choice one') 
choice2 = Button(root, text='Choice one plus one') 
choice3 = Button(root, text='Choice 3') 
choice4 = Button(root, text='Choice eight divided by 2') 

que_labl.grid(row=0, columnspan=2) 
choice1.grid(row=2, column=0, sticky=E+W) 
choice2.grid(row=2, column=1, sticky=E+W) 
choice3.grid(row=3, column=0, sticky=E+W) 
choice4.grid(row=3, column=1, sticky=E+W) 

root.mainloop() 
+0

爲迂腐,粘性值不需要包含N和S爲這個具體問題。 –

+0

@BryanOakley謝謝你,這是一個表格解決方案。 – Zety