Open In App

Looping through buttons in Tkinter

Last Updated : 26 Mar, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, let’s see how we can loop through the buttons in Tkinter.

Stepwise implementation:

Step 1: Import the Tkinter package and all of its modules and create a root window (root = Tk()).

Python3




# Import package and it's modules
from tkinter import *
  
# create root window
root = Tk()
  
# root window title and dimension
root.title("GeekForGeeks")
  
# Set geometry (widthxheight)
root.geometry('400x400')
  
# Execute Tkinter
root.mainloop()


Output

Step 2: Now let’s add a Entry() class and will display the button name as soon as one of the buttons is clicked.

Python3




# Import package and it's modules
from tkinter import *
  
# create root window
root = Tk()
  
# root window title and dimension
root.title("GeekForGeeks")
  
# Set geometry (widthxheight)
root.geometry('400x400')
  
# Entry Box
text = Entry(root, width = 30, bg = 'White')
text.pack(pady = 10)
  
# Execute Tkinter
root.mainloop()


Output  

Step 3: Now let’s create an empty dictionary (button_dict) to save all the button objects and a list consisting of names of all the buttons. Now loop over each item of the list to create an button object of it and store it in the dictionary. For the button command, create a function named ‘action’ and for each button call the text_update() function to update the changes in the entry in Entry() object created earlier.

Python3




# Import package and it's modules
from tkinter import *
  
  
# text_update function
def text_updation(language):
    text.delete(0, END)
    text.insert(0, language)
  
# create root window
root = Tk()
  
# root window title and dimension
root.title("GeekForGeeks")
  
# Set geometry (widthxheight)
root.geometry('400x400')
  
# Entry Box
text = Entry(root, width=30, bg='White')
text.pack(pady=10)
  
# create buttons
button_dict = {}
words = ["Python", "Java", "R", "JavaScript"]
for lang in words:
    
    # pass each button's text to a function
    def action(x = lang): 
        return text_updation(x)
        
    # create the buttons 
    button_dict[lang] = Button(root, text = lang,
                               command = action)
    button_dict[lang].pack(pady=10)
  
# Execute Tkinter
root.mainloop()


Output

loop through button tkinter



Previous Article
Next Article

Similar Reads

Dynamically Resize Buttons When Resizing a Window using Tkinter
Prerequisite: Python GUI – tkinter Button size is static, which means the size of a button cannot be changed once it is defined by the user. The problem here is while resizing the window size, it can affect the button size problem. So the solution here is, make a dynamic button, which means the button size will change as per window size. Let's unde
2 min read
How to make Rounded buttons in Tkinter
Tkinter is a Python module that is used to create GUI (Graphical User Interface) applications with the help of a variety of widgets and functions. Like any other GUI module, it also supports images i.e you can use images in the application to make it more attractive. In this article, we will discuss How to make a Rounded Button in Tkinter, There is
2 min read
PyQt5 QSpinBox - Adding looping
In this article we will see how we can add looping to the spin box, when we create a spin box by default when it reaches the maximum value it can't be incremented any more similarly when it reaches minimum value it can't be decremented any more. By adding looping to the spin box the values repeat it self after the maximum or minimum value is reache
2 min read
PyQt5 StringSpinBox - Looping the strings
In this article we will see how we can make the string values repeat themselves when the last value reaches it should go back to the first value similarly when value is decremented after first value it should go back to the last value. In order to do this we have to change the custom class code of the StringSpinBox. Below is the code for the custom
4 min read
Looping Techniques in Python
Python supports various looping techniques by certain inbuilt functions, in various sequential containers. These methods are primarily very useful in competitive programming and also in various projects that require a specific technique with loops maintaining the overall structure of code. A lot of time and memory space is been saved as there is no
6 min read
Difference Between The Widgets Of Tkinter And Tkinter.Ttk In Python
Python offers a variety of GUI (Graphical User Interface) frameworks to develop desktop applications. Among these, Tkinter is the standard Python interface to the Tk GUI toolkit. Tkinter also includes the 'ttk' module, which enhances the visual appeal and functionality of the applications. In this article, we will cover the differences, providing a
4 min read
PyQt5 QCalendarWidget - Setting Border to the tool buttons
In this article we will see how we can set border to the tool buttons of the QCalendarWidget. Tool buttons are the buttons available at the top i.e buttons to go to left and right page, setting border to QCalendarWidget is not like setting border to the other widgets, calendar is widget which has many child i.e component we can set border to indepe
2 min read
Python | Working with buttons in Kivy
Kivy is a platform independent GUI tool in Python. As it can be run on Android, IOS, linux and Windows etc. Kivy provides you the functionality to write the code for once and run it on different platforms. It is basically used to develop the Android application, but it does not mean that it can not be used on Desktops applications. Kivy Tutorial -
5 min read
Python | Working with buttons in Kivy with .kv file
Kivy is a platform independent GUI tool in Python. As it can be run on Android, IOS, linux and Windows etc. It is basically used to develop the Android application, but it does not mean that it can not be used on Desktops applications. Button: The Button is a Label with associated actions that are triggered when the button is pressed (or released a
4 min read
How to create Buttons in a game using PyGame?
Pygame is a Python library that can be used specifically to design and build games. Pygame only supports 2D games that are build using different shapes/images called sprites. Pygame is not particularly best for designing games as it is very complex to use and lacks a proper GUI like unity gaming engine but it definitely builds logic for further lar
3 min read
Practice Tags :