Create Labels and Text Fields

Previous
Next

grid() layout:

  • grid() function arrange the components in a table format(2 dimensional).
  • We need to specify row value and column value of component
from tkinter import Tk, Label, Button, Entry

def func():
    name = e.get()
    print("Hello " + name + ", welcome to GUI")
    return

window = Tk()
window.title("Grid Layout")

l = Label(window, text="Enter Name :", font="Times 25")
l.grid(row=0, column=0)

e = Entry(window, font="Times 25", fg="red")
e.grid(row=0, column=1)

b = Button(window, text="Click", font="Times 20", command=func)
b.grid(row=0, column=2)

StringVar class:

  • It is belongs to tkinter module
  • It is used to set dynamic text(values) to components
  • To assign StringVar object we use “textvariable” argument of that component instead of “text” argument.
from tkinter import Tk, Label, Button, Entry, StringVar

def func():
    name = e.get()
    msg.set("Hello " + name + ", Welcome")
    return

window = Tk()
msg = StringVar()
window.title("Dynamic Text")
window.geometry("750x300")

l = Label(window, text="Enter Name :", font="Times 20")
l.place(x=50, y=50)

e = Entry(window, font="Times 20", fg="red")
e.place(x=250, y=50)

b = Button(window, text="Click", font="Times 15", command=func)
b.place(x=600, y=50)

res = Label(window, textvariable=msg, font="Times 20", fg="blue")
res.place(x=150, y=175)
Previous
Next

Add Comment

Courses Enquiry Form