- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to create Tkinter buttons in a Python for loop?
Tkinter Button widgets are very useful in terms of handling events and performing actions during the execution of an application. We can create Tkinter Buttons using the Button(parent, text, option..) constructor. Using the constructor, we can create multiple buttons within the loop.
Example
In this example, we will create multiple buttons in the range using a Python for loop.
#import required libraries from tkinter import * from tkinter import ttk #Create an instance of Tkinter frame win= Tk() #Set the geometry of the window win.geometry("750x250") #Create a LabelFrame labelframe= LabelFrame(win) #Define a canvas in the window canvas= Canvas(labelframe) canvas.pack(side=RIGHT, fill=BOTH, expand=1) labelframe.pack(fill= BOTH, expand= 1, padx= 30, pady=30) #Create Button widget in Canvas for i in range(5): ttk.Button(canvas, text= "Button " +str(i)).pack() win.mainloop()
Output
Running the above code will display a window containing some buttons inside a LabelFrame object.
- Related Articles
- How to create a triangle using Python for loop?
- Create multiple buttons with "different" command function in Tkinter
- How to generate Tkinter Buttons dynamically?
- How to convert a Python for loop to while loop?
- How to create a lambda inside a Python loop?
- How to break a for loop in Python?
- How do you create a Tkinter GUI stop button to break an infinite loop?
- How to use for loop in Python?
- Python - How to convert this while loop to for loop?
- How to create shadow in Android Buttons?
- How to run an infinite loop in Tkinter?
- How to stop a loop with a stop button in Tkinter?
- How to create a form with custom buttons in HTML
- How to handle exception inside a Python for loop?
- Removing minimize/maximize buttons in Tkinter

Advertisements