Abraham Abraham - 3 months ago 28
Python Question

How to put widgets together using Tkinter in Python?

This is the code that I wrote to have two button and a text entry in my GUI:


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)

And this is the result:
enter image description here

But what I want is this:

enter image description here

How can I have it? (a label above the text entry is ideal too)

Is there any way to make the text entry read only?


You can use columnspan option of grid() to make text extend more than one column.
To make text read-only, simply set state option of text widget to "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

About label, just place it to row=1 and move text to row=2.