2016-11-30 51 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

相关问题