[Python] Tkinter template

Introduction

Here's a classic way to classify Tkinter, a Python GUI tool.

This is an introductory article on how to create an Image Viewer using this template. (Addition) Create Image Viewer with Tkinter

Classification template

  1. Application class Widget settings & placement, define Callback function
  2. main function
import tkinter as tk
from tkinter import ttk

class Application(tk.Frame):
    def __init__(self,master):
        super().__init__(master)
        self.pack()

        self.master.geometry("300x300")
        self.master.title("Tkinter with Class Template")

        self.create_widgets()

    def create_widgets(self):
        pass

    def callBack(self):
        pass

def main():
    root = tk.Tk()
    app = Application(master=root)#Inherit
    app.mainloop()

if __name__ == "__main__":
    main()

Execution result

Tkinter-template.png

Tkinter procedural programming example

Create a GUI that performs the following operations when the button is pressed.

  1. The message changes
  2. Output the text entered in Entry This time, I will write in the procedure procedure program. It's intuitive and easy to understand, but it has the drawback of being difficult to maintain and repair as the project grows.
import tkinter as tk
from tkinter import ttk

#Generate root instance
root = tk.Tk()
root.geometry("300x300")
root.title("Tkinter without Class")

#Define Button Click Event Function

def say_Hello():
    print("Hello World") # on python console
    label_hello.configure(text="I Have benn Clicked!")
    print(name.get())
    label_name.configure(text = name.get())

#Define Button Widget

button_hello = ttk.Button(master=root)
button_hello.configure(text="Hello World")
button_hello.configure(command = say_Hello)
button_hello.pack()

#Define Label Widget
label_hello = ttk.Label(master=root)
label_hello.configure(text = 'A Label')
label_hello.pack()

#Define Enntry Widget
name= tk.StringVar()
entry_name = ttk.Entry(master=root)
entry_name.configure(textvariable = name)
entry_name.pack()

#Define Label Widget2
label_name = ttk.Label(master=root)
label_name.configure(text = 'Please input something in Entry')
label_name.pack()


# Start GUI
root.mainloop()

Tkinter object-oriented programming

Here, we will change the procedural programming code in the previous chapter to a template for classification. Be careful with self.

import tkinter as tk
from tkinter import ttk

class Application(tk.Frame):
    def __init__(self,master):
        super().__init__(master)
        self.pack()

        self.master.geometry("300x300")
        self.master.title("Tkinter with Class")

        self.create_widgets()


    # Create Widgets function
    def create_widgets(self):
        #Button
        self.button_hello = ttk.Button(self)
        self.button_hello.configure(text="Hello World")
        self.button_hello.configure(command = self.say_Hello) #do not forget to add self!
        self.button_hello.pack()

        #Label
        self.label_hello = ttk.Label(self)
        self.label_hello.configure(text='A Label')
        self.label_hello.pack()

        #Entry
        self.name = tk.StringVar()
        self.entry_name = ttk.Entry(self)
        self.entry_name.configure(textvariable = self.name)
        self.entry_name.pack()

        #Label2
        self.label_name=ttk.Label(self)
        self.label_name.configure(text = 'Please input something in Entry')
        self.label_name.pack()

    # Event Callback Function
    def say_Hello(self):
        print("Hello, World")  # on python console
        self.label_hello.configure(text="I Have benn Clicked!")
        print(self.name.get())
        self.label_name.configure(text=self.name.get())



def main():
    root = tk.Tk()
    app = Application(master=root)#Inherit class inheritance!
    app.mainloop()

if __name__ == "__main__":
    main()

Execution result

Tkinter_Blog.png

Summary

I've found that using classes makes the Python code more structured and easier to read. With this, we will continue to expand. I felt like I was finally able to see the world of Python.

Reference material

  1. [Python] Template using Tkinter (classification method)
  2. A Simple Hello World Program
  3. python tkinter documentation "A Simple Hello World Program" is too difficult
  4. Python GUI with Tkinter - 8 - Using Classes (Youtube)
  5. Create Image Viewer with Tkinter

Recommended Posts

[Python] Tkinter template
python argparse template
Competitive Pro Template (Python)
Python data analysis template
Jinja2 | Python template engine
Python Tkinter Primer Note
python unit test template
Python template engine empy
Programming with Python and Tkinter
Python
How to update Python Tkinter to 8.6
Python Tkinter notes (for myself)
Python template for Codeforces-manual test-
Run Label with tkinter [Python]
[Python] Competitive template [At Coder]
template
Python Design Pattern --Template method
[Python] Show multiple windows in Tkinter
Competitive programming, coding test template: Python3
Create a python GUI using tkinter
Template AtCoder ABC 179 Python (A ~ E)
[Python] Creating multiple windows with Tkinter
GUI creation in python using tkinter 2
kafka python
Play video with sound with python !! (tkinter / imageio)
Tkinter begins
Python basics ⑤
python + lottery 6
Built-in python
Python comprehension
Python technique
About building GUI using TKinter of Python
Studying python
Python 2.7 Countdown
Python memorandum
Python FlowFishMaster
GUI creation in python using tkinter part 1
Python service
Periodic execution processing when using tkinter [Python3]
python function ①
Python basics
Python memo
ufo-> python (3)
Python comprehension
install python
Python Singleton
Python basics ④
Python Memorandum 2
python memo
Python Jinja2
Python increment
python tips
Installing Python 3.4.3.
Try python
Python memo
Python iterative
Python algorithm
[Python] Variables
Python functions
Python sys.intern ()
Django # 2 (template)