In this article, I would like to give a simple tutorial on how Python Tkinter Dynamic Label works.If you are not familiar with Python GUI Tkinter then please refer the previous articles to get started 🙂
As usual, I would like to write some simple code to explain how to write Dynamic content (That really changes) and Creating Buttons in the Tkinter Label.
Here is my code :
Python Tkinter Dynamic Label Tutorial
I don’t want to explain from start to end because I already explained the basic Python Tkinter Syntax in my previous articles.please refer them.
If you followed my previous articles, we can recognize something new in this tutorial that is main.title() which is used to set the title of the Tkinter window.
text_label = tk.Label(main, fg="black")
In this line, we are going to create a new Label that is assigned for text(font color is black) which can be changed later dynamically itself by calling another method.
We are calling the inc_label function with text_label as a parameter so that whatever we write in this method will be printed on the window with black color fonts.
As we need to change the content in the text_label dynamically, we are writing this code.Firstly, We assigned a counter variable to zero and defined a method called inc_label with one parameter.In the next lin, we have another method called count which counts the numbers until we stop it. A global variable can be accessible not only in that specific method but also at anywhere in our script.
Counter variable is to be incremented by 1 every time if we call this function.Here we are using a label.config that has one parameter (text) just to print the counter value on the screen.By default, it is the Int type.so We need to convert to the string so we have to typecast (str(counter)) it.
label.after() has two parameters one is for the time limit and another is count.The last line is used to call the same function so that counter value is getting incremented every time by 1.
Python Tkinter Button Label – How To Create Button
Creating Button in the python could be very easy for our tutorial.We are using tk.Button method that has four parameters in our case.The first one parameter always should point to the parent window,the second one is the text that we wish on the button.Third for width and fourth is the command parameter where we can give different commands to execute when the button is clicked. This time we are saying that this main (parent window) should be terminated or destroyed.
That’s simple.Let’s run it.
Please, Like Our Facebook Page For More Updates and Comment Here.