Getting Started with Tkinter
1. Import tkinter package and all of its modules.
2. Create a root window. Give the root window a title(using title()) and dimension(using geometry()). All other widgets will be inside the root window.
3. Use mainloop() to call the endless loop of the window. If you forget to call this nothing will appear to the user. The window will wait for any user interaction till we close it.
Python3
# Import Module from tkinter import * # create root window root = Tk() # root window title and dimension root.title( "Welcome to GeekForGeeks" ) # Set geometry (widthxheight) root.geometry( '350x200' ) # all widgets will be here # Execute Tkinter root.mainloop() |
Output
4. We’ll add a label using the Label Class and change its text configuration as desired. The grid() function is a geometry manager which keeps the label in the desired location inside the window. If no parameters are mentioned by default it will place it in the empty cell; that is 0,0 as that is the first location.
Python3
# Import Module from tkinter import * # create root window root = Tk() # root window title and dimension root.title( "Welcome to GeekForGeeks" ) # Set geometry(widthxheight) root.geometry( '350x200' ) #adding a label to the root window lbl = Label(root, text = "Are you a Geek?" ) lbl.grid() # Execute Tkinter root.mainloop() |
Output
5. Now add a button to the root window. Changing the button configurations gives us a lot of options. In this example we will make the button display a text once it is clicked and also change the color of the text inside the button.
Python3
# Import Module from tkinter import * # create root window root = Tk() # root window title and dimension root.title( "Welcome to GeekForGeeks" ) # Set geometry(widthxheight) root.geometry( '350x200' ) # adding a label to the root window lbl = Label(root, text = "Are you a Geek?" ) lbl.grid() # function to display text when # button is clicked def clicked(): lbl.configure(text = "I just got clicked" ) # button widget with red color text # inside btn = Button(root, text = "Click me" , fg = "red" , command = clicked) # set Button grid btn.grid(column = 1 , row = 0 ) # Execute Tkinter root.mainloop() |
Output:
6. Using the Entry() class we will create a text box for user input. To display the user input text, we’ll make changes to the function clicked(). We can get the user entered text using the get() function. When the Button after entering of the text, a default text concatenated with the user text. Also change button grid location to column 2 as Entry() will be column 1.
Python3
# Import Module from tkinter import * # create root window root = Tk() # root window title and dimension root.title( "Welcome to GeekForGeeks" ) # Set geometry(widthxheight) root.geometry( '350x200' ) # adding a label to the root window lbl = Label(root, text = "Are you a Geek?" ) lbl.grid() # adding Entry Field txt = Entry(root, width = 10 ) txt.grid(column = 1 , row = 0 ) # function to display user text when # button is clicked def clicked(): res = "You wrote" + txt.get() lbl.configure(text = res) # button widget with red color text inside btn = Button(root, text = "Click me" , fg = "red" , command = clicked) # Set Button Grid btn.grid(column = 2 , row = 0 ) # Execute Tkinter root.mainloop() |
Output:
7. To add a menu bar, you can use Menu class. First, we create a menu, then we add our first label, and finally, we assign the menu to our window. We can add menu items under any menu by using add_cascade().
Python3
# Import Module from tkinter import * # create root window root = Tk() # root window title and dimension root.title( "Welcome to GeekForGeeks" ) # Set geometry(widthxheight) root.geometry( '350x200' ) # adding menu bar in root window # new item in menu bar labelled as 'New' # adding more items in the menu bar menu = Menu(root) item = Menu(menu) item.add_command(label = 'New' ) menu.add_cascade(label = 'File' , menu = item) root.config(menu = menu) # adding a label to the root window lbl = Label(root, text = "Are you a Geek?" ) lbl.grid() # adding Entry Field txt = Entry(root, width = 10 ) txt.grid(column = 1 , row = 0 ) # function to display user text when # button is clicked def clicked(): res = "You wrote" + txt.get() lbl.configure(text = res) # button widget with red color text inside btn = Button(root, text = "Click me" , fg = "red" , command = clicked) # Set Button Grid btn.grid(column = 2 , row = 0 ) # Execute Tkinter root.mainloop() |
Output
This simple GUI covers the basics of Tkinter package. Similarly, you can add more widgets and change their configurations as desired.
Create First GUI Application using Python-Tkinter
We are now stepping into making applications with graphical elements, we will learn how to make cool apps and focus more on its GUI(Graphical User Interface) using Tkinter.
Contact Us