码迷,mamicode.com
首页 > 其他好文 > 详细

Creating a better Hello World Tkinter

时间:2020-07-26 00:40:59      阅读:61      评论:0      收藏:0      [点我收藏+]

标签:font   tle   false   title   value   man   import   command   nconf   

Creating a GUI the way we just did works okay for very small scripts, but a much more scalable approach is to subclass Tkinter widgets to create component widgets that we will then assemble into a completed application.  

Subclassing is simply a way of creating a new class based on an existing one, adding or changing only what is different in the new class. We will use subclassing extensively in this book to extend the functionality of Tkinter widgets.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
""" Code for << Python GUI Programming with Tkinter >> by Alan D.Moore """
""" A better Hello World for Tkinter """
import tkinter as tk
from tkinter import ttk

# (1) define My widgets module
class HelloView(tk.Frame):
    """ A friendly little module """
    # (1) define initial function
    def __init__(self, parent, *args, **kwargs):
        super().__init__(parent, *args, **kwargs)   # initial parent class
        # initial children class
        # (1) define variables
        self.name = tk.StringVar()              # define tkinter String variable
        self.hello_string = tk.StringVar()      # define tkinter String variable
        self.hello_string.set("Hello World")    # Assign Hello World to self.hello_string
        # (2) initial widgets
        name_label = ttk.Label(self, text = "Name:")    # define a Label widget
        name_entry = ttk.Entry(self, textvariable = self.name)  # define a entry widget, entry value to self.name
        ch_button = ttk.Button(self, text = "Change", command = self.on_change) # define a button named Change.call function on_change
        hello_label = ttk.Label(self, textvariable  = self.hello_string, font = ("TkDefaultFont", 32), wraplength = 600)
        # (3) place widgets
        name_label.grid(row = 0, column = 0, sticky = tk.W) # place in (0,0), west West direction
        name_entry.grid(row = 0, column = 1, sticky = (tk.W + tk.E))
        ch_button.grid(row = 0, column = 2, sticky = tk.E)
        hello_label.grid(row = 1, column = 0, columnspan = 3) # place in (1,0~2), Occupies three grids
        # (4) Adjust the size of rows and columns
        self.columnconfigure(1, weight = 1)
        
    # (2) define other function
    def on_change(self):
        if self.name.get().strip():
            self.hello_string.set("Hello " + self.name.get())
        else:
            self.hello_string.set("Hello World")
            
# (2) define My Application
class MyApplication(tk.Tk):
    """ Hello World Main Application """
    # (1) define initial function
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.title("Hello Tkinter")     # windown name
        self.geometry("400x300")      # windown size  (width x height)
        self.resizable(width = False, height = False)

        # adding My widgets module and setting the place
        HelloView(self).grid(sticky = (tk.E + tk.W + tk.N + tk.S))
        self.columnconfigure(0,weight = 1)
        
                            
# (3) Main function
if __name__ == __main__:
    app = MyApplication()
    app.mainloop()
    
        

 

 

Creating a better Hello World Tkinter

标签:font   tle   false   title   value   man   import   command   nconf   

原文地址:https://www.cnblogs.com/wqq2000happy/p/13377162.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!