这是我编写的代码,在我的GUI中有两个按钮和一个文本条目:
#!/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()
但我想要的是:
我怎么能拥有它? (文本条目上方的标签也是理想的)
有没有办法让文字条目只读?
答案 0 :(得分:2)
您可以使用columnspan
option of grid()使文字扩展多个列
要将文字设为只读,只需将state
option 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
。