
- 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 I create an automatically updating GUI using Tkinter in Python?
GUI window has many controls such as labels, buttons, text boxes, etc. We may sometimes want the content of our controls such as labels to update automatically while we are viewing the window.
We can use after() to run a function after a certain time. For example, 1000 milliseconds mean 1 second. The function which we call continuously after a certain amount of time will update the text or any updation you want to happen.
We have a label on our window. We want the text of the label to update automatically after 1 second. To keep the example easy, suppose we want the label to show some number between 0 and 1000. We want this number to change after each 1 second.
We can do this by defining a function that will change the text of the label to some random number between 0 and 1000. We can call this function continuously after an interval of 1 second using the after().
Example
from Tkinter import * from random import randint root = Tk() lab = Label(root) lab.pack() def update(): lab['text'] = randint(0,1000) root.after(1000, update) # run itself again after 1000 ms # run first time update() root.mainloop()
This will automatically change the text of the label to some new number after 1000 milliseconds. You can change the time interval according to need. The update function can be modified to perform the required updation.
root.after(1000,update)
This line of the code performs the main function of recalling the function update().
The first parameter in root.after() specifies the time interval in milliseconds after which you want the function to be recalled.
The second parameter specifies the name of the function to be recalled.
- Related Articles
- How do I create an automatically updating GUI using Tkinter?
- How to create an impressive GUI in Python using Tkinter?
- How do you create a Tkinter GUI stop button to break an infinite loop?
- Simple GUI calculator using Tkinter in Python
- How to create a directly-executable cross-platform GUI app using Python(Tkinter)?
- How do I create child windows with Python tkinter?
- How do I create a popup window using Tkinter?
- How do I create a popup window using Tkinter Program?
- Create a GUI to check domain availability using Tkinter
- Create a GUI to Get Domain Information using Tkinter
- How to build a simple GUI calculator using tkinter in Python
- How to add PDF in Tkinter GUI Python?
- How do I create a popup window in Tkinter?
- How do I create a date picker in tkinter?
- Ratio Calculator GUI using Tkinter
