如何在Python中使用Tkinter将小部件放在一起?

时间:2016-11-30 09:06:54

标签: python tkinter

这是我编写的代码,在我的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()

这就是结果: enter image description here

但我想要的是:

enter image description here

我怎么能拥有它? (文本条目上方的标签也是理想的)

有没有办法让文字条目只读?

1 个答案:

答案 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