tkinter provides us with a variety of common GUI elements which we can use to build our interface – such as buttons, menus and various kinds of entry fields and display areas.
Import the Tkinter package, we will create a window and set its title:
from tkinter import *
window = Tk()
window.title("Welcome to LikeGeeks app")
window.mainloop()
Size Window Set the size of a window
window.geometry('350x200')
Create a label widget
lbl = Label(window, text="Hello")
Set its position in the form using the grid function with its location, in this way:
lbl.grid(column=0, row=0)
Set label font size
Change font type
lbl = Label(window, text="Hello", font=("Arial Bold", 24))
Add a button
btn = Button(window, text="Click Me")
btn.grid(column=1, row=0)
Manage the event click of a button
- We will write the function that we need to execute when the button is clicked:
def clicked():
lbl.configure(text="Button was clicked !!")
Link the event to the button
btn = Button(window, text="Click Me", command=clicked)
- Create a text box using the Tkinter Entry class in this way:
txt = Entry(window,width=10)
To use the entered text, it is saved by a function def clicked ()
def clicked():
res = "Welcome to " + txt.get()
lbl.configure(text= res)
chk = Checkbutton(window, text='Option')
chk_state = IntVar()
chk_state.set(0) #uncheck
chk_state.set(1) #check
rad1 = Radiobutton(window,text='First', value=1, variable=selected)
Get the value of the radio button
def clicked():
print(selected.get())
from tkinter import messagebox
messagebox.showinfo('Message title','Message content')
Shows warning message
messagebox.showwarning('Message title', 'Message content')
Shows error message
messagebox.showerror('Message title', 'Message content')
A Spinbox widget and pass the parameters from_ and to to specify the range of numbers of the Spinbox.
spin = Spinbox(window, from_=0, to=100, width=5)
To add a menu bar, you can use the menu class:
from tkinter import Menu
menu = Menu(window)
menu.add_command(label='File')
window.config(menu=menu)
You can add the menu items, in any menu, with the add_cascade () function:
menu.add_cascade(label='File', menu=new_item)
Thank you