Topic: Python Set Label Text on Button Click tkinter GUI Program
Explanation of Code in the Image
- Imports tkinter module, use tk as an alias. Because tk is short and easy to use in code.
- Header of main() function.
- Indented body of main() function . Create main window or Root window of the application.
- Set title of the main window.
- Set size of the main window.
- Create a label with empty text.
- Place label in the main window at x,y location.
- Define and set a txt variable to “Easy Code Book” string literal.
- Define btn1_click() function to handle the button click event.
- Set the text of label to txt variable.
- Create a button and bind it to btn1_click event handler by setting command option.
- Place button in the root window at x,y position.
- When this line of code will execute, it will start the mainloop that is event loop. So that the output root window is shown to the user. It will wait for any actions called events performed by the user and the code attached, if any, will respond to those events. For example, if we click on close button, the window will be closed.
- Call to main() function. Now the execution of the program will start and the root window will be displayed on the screen.
1,993 total views, 3 views today