
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How do you run your own code alongside Tkinter's event loop?
Tkinter is widely used to create and develop GUI based applications and games. Tkinter provides its window or frame where we execute our programs and functions along with other attributes.
Let us consider that we are working with a particular application and we want to write the changes in the code while running the application. Tkinter provides a callback method which can be used to run the window while iterating over it. We can keep running the window using the after(duration,task) method that will basically run the changes after a duration.
In this example, we will create a window which prints the numbers in the range (0 to 9) whilst running the main window or frame.
Example
#Import the required libraries from tkinter import * from tkinter import messagebox #Create an instance of tkinter frame or window win= Tk() #Set the geometry win.geometry("700x200") #Define the function for button def some_task(): for i in range(10): print(i) #Recursively call the function win.after(2000, some_task) #Keep Running the window win.after(2000, some_task) win.mainloop()
Output
Running the above code will keep printing the numbers in the range (0 to 9) on the console, and along with that, it will display the main window.
0 1 2 3 4 5 6 7 8 9 …….
- Related Articles
- How to run an infinite loop in Tkinter?
- Can you build your own sundial?
- How do you create a Tkinter GUI stop button to break an infinite loop?
- Manipulate for loop to run code with specific variables only PHP?
- Can you own Crypto in your Roth IRA
- How do you loop through a C# array?
- How do I handle the window close event in Tkinter?
- Why does C code run faster than Python's?
- How did you know the right time to start your own business?
- How do you make code reusable in C#?
- How do you Loop Through a Dictionary in Python?
- How do you create a clickable Tkinter Label?
- How to run matplotlib in Tkinter?
- How do you code a vending machine in Python?
- How do you overlap widgets/frames in Python tkinter?
