This is the 21st day of my participation in the August Text Challenge.More challenges in August

Review and

We took a basic look at GUI programming in the initial GUI programming installment. Provides the GUI standard library Tkinter library for Python, and creates GUI applets for initial use.

GUI graphical user interface is a component (module) composition, our image interface procedures are composed of a variety of components

  • Component: A module with a specific function, similar to the small parts of a building block
  • Container: You can put multiple different components together as a whole

In this installment, we will continue to learn GUI programming related knowledge and programming specifications,Let’s go~

1. GUI programming class diagram

Let’s take a look at Tkinter’s GUI component diagram

📣 Important Note:

  1. The diagram above shows the inheritance diagram of related classes, with subclasses inheriting from their parent class from right to left.

    (1) For example, component Label inherits ->Widget class -> Basewidget class -> Misc class -> Object parent class

    A subclass has all the attributes and methods of its parent class

  2. Misc and Wim

    Tkinter’s GUI components have two parent classes that directly inherit from the Object class

    • Misc is the root parent of all components

    • Wim mainly provides functions to communicate with the window manager

  3. TK

    • The class TK is derived from Misc and Wim.

    • Represents the main window of an application that requires direct or indirect use of TK

    • Generally defined in the program

      Root = TK()Copy the code
  4. The Pack class, the Place class, and the Grid class are common layout managers

    • The layout manager manages the size and location of components
    • You can arrange the components in a container properly
  5. Basewidget is the parent class of all components

  6. Widgets are the parent class of all components

    • Widgets have four parent classes: BaseWidget, Pack, Grid, and Place

    • All GUI components have all the properties and methods of the four parent classes

2. Common component methods

Tkinter class The name of the Introduction to the
Toplevel At the top Container class, which can be used to provide separate containers for other components, Toplevel is somewhat similar to a window
Button button Represents the button component
Canvas The canvas Provides drawing functions including lines, rectangles, ellipses, polygons, bitmaps, etc
Checkbutton Check box Check boxes that can be checked by the user
Entry Single-line input box The content that the user can enter
Frame The container Used to load other GUI components
Label The label Display non-editable text or ICONS
LabelFrame The container Also a container component, like a Frame, that supports adding titles
Listbox List box Lists multiple options for the user to choose from
Menu The menu The menu component
Menubutton The menu button Buttons to contain menus (including dropdown, cascade)
OptionMenu The menu button A subclass of Menubutton, which also stands for Menubutton, which opens a menu
Message Message box Similar to tags, multiple lines of text can be displayed

3.GUI object-oriented writing

We have already used the Tkinter class to create a simple GUI program. The steps are as follows:

  1. Create the root window
  2. Create components
  3. Layout management
  4. Import events

. All of this is procedural programming

However, our actual work, the development of the program is more complex

GUI programming we also need to use object-oriented approach to programming, more reasonable organization of our code

📣GUI object-oriented programming key points:

Official website DEMO version:

  • Use the Application class to organize individual GUI programs
  • The Application class inherits the Frame class and all its parent class features
  • Use the __init__() constructor to initialize the object in the window
  • Use the createWidgets () method to create objects in the window
  • Create the main window root
  • Run the call Application to invoke the GUI object
  • Circular event management interacts with the user

🔔Frame A Frame is a Tkinter component that represents a rectangular area. A Frame is generally used as a container for placing other components to achieve a complex layout

⌛ Object-oriented code logic split diagram:

📝 According to the DEMO version recommended by the official website, we will use object-oriented thought to achieve GUI procedures

from tkinter import * class Application(Frame): def __init__(self,master=None): # super() represents the definition of the parent class, Super ().__init__(master) self.master = master self.pack() self.createWidget () def createWidget (self): self.but1 = Button(self) self.but1["text"] = "add" self.but1.pack() self.but1["command"] = self.addinfo def addinfo(self): Root = Tk() root. Geometry ("400x100+200+300") root. Title ("MyfirstAPP") app = Application(master=root) root.mainloop()Copy the code

conclusion

In this installment, we will learn the object-oriented writing of GUI programming to make our code cleaner and improve code reuse.

GUI object-oriented programming inherits the Frame class to use constructors instead of repeatedly creating component objects. The component operations are all encapsulated in the CreateWidget method, and the Application object is called directly by the Master.

The above is the content of this issue, welcome big guys to point up comments and corrections, see you next time ~ღ(´ ᴗ · ‘) than heart 🌹🌹 ᴗ