2016-11-30 76 views
-2

這是我寫的有兩個按鈕,並在我的GUI文本輸入的代碼:如何使用Python中的Tkinter將小部件放在一起?

#!/usr/bin/python 

import Tkinter 
from Tkinter import * 

top = Tkinter.Tk() 
b1 = Button (top, text = "Hack it!", height = 10, width = 20) 
b2 = Button (top, text = " Clone! ", height = 10, width = 20) 
t = Text(top,width=60,height=40) 
b1.grid(row=0, column=0) 
b2.grid(row=0, column=1) 
t.grid(row=1) 
top.mainloop() 

這是結果: enter image description here

但我想是這樣的:

enter image description here

我該怎麼辦? (文本條目上方的標籤也是理想的)

有什麼辦法讓文本條目只讀嗎?

+2

您可以在這裏找到大部分Tkinter小部件的屬性和方法:http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/index.html –

回答

2

您可以使用columnspanoption of grid()使文本擴展多個列。
要使文本爲只讀,只需將stateoption of text widget設置爲"disabled"即可。

import Tkinter as tk 

top = tk.Tk() 
b1 = tk.Button(top, text="Hack it!", height=10, width=20) 
b2 = tk.Button(top, text=" Clone! ", height=10, width=20) 
t = tk.Text(top, width=60, height=40, state="disabled") #makes text to be read-only 
b1.grid(row=0, column=0) 
b2.grid(row=0, column=1) 
t.grid(row=1, columnspan=2) #this makes text to span two columns 
top.mainloop() 

關於標籤,只需將它放在row=1和移動文本row=2

相關問題