
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Generate Tkinter Buttons Dynamically
In this article, we will see how to create buttons dynamically in a tkinter window. Creating buttons dynamically means customizing the buttons and their functionality by adding events to them.
First, we will import the tkinter library in the notebook, then we will create an instance using the Button function which takes parameters such as parent or root of the window, textvariable which is the value to assign in each button and command.
Syntax
Button(parent, textvariable, command)
Example
from tkinter import * import tkinter as tk # create an instance of tkinter win = tk.Tk() #Define the size of the window win.geometry("700x200") #Name the title of the window win.title("www.tutorialspoint.com") # number of buttons n=10 #Defining the row and column i=3 #Iterating over the numbers till n and #creating the button for j in range(n): mybutton= Button(win, text=j) mybutton.grid(row=i, column=j) # Keep the window open win.mainloop()
Output
Running the above code in tkinter notebook will generate the following output.
Advertisements