2021-07-11 Python Basics - GUI programming

catalogue

1, Overview of GUI user interface programming:

2, Common GUI libraries include Tkinter, wxpthon, PyQT

#Tkinter Library

1. Common methods: mainloop() main event loop execution

2. Execution steps:

3. Common components

4. Component properties: each component has its own properties and methods

5. Canvas components

1, Overview of GUI user interface programming:

  • GUI - graphical user interface programming, also known as graphical user interface
  • GUI programming is similar to "building blocks", putting components (widget s) into the window.
  • There are various components in the window, such as buttons, menus, editing area... And it becomes a complete program by adding "event handling".
  • People do not need to memorize and input cumbersome commands, but can operate the interface directly through the mouse or keyboard.

2, Common GUI libraries include Tkinter, wxpthon, PyQT

#Tkinter Library

  • It is a python standard GUI library, which supports cross platform GUI program development. At the same time, it is suitable for small GUI program programming
  • Lack of appropriate visual interface design tools
  • You need to write code to complete the layout and design of the window

1. Common methods: mainloop() main event loop execution

2. Execution steps:

  • Import Tkinter module: from Tkinter import * or} import Tkinter
  • Main window for creating GUI application: TK = Tkinter Tk()
  • Add various components
  • Each event response is triggered by the user entering the main event loop

3. Common components

Common GUI components
assemblyChinese nameSpecific description
CanvascanvasIt provides drawing function, which can include graphics or bitmap to realize customized window components
ButtonButtonIt has mouse skimming, pressing, releasing and keyboard operation events
LabellabelDisplay text or pictures
EntryText boxSingle line text area to collect keyboard input
TextText fieldMultiline text area to collect or display text entered by users
CheckButtonCheckbox A group of boxes that can select multiple items
RadiobuttonRadio A group of buttons. Only one item can be selected
MenumenuClick the menu to pop up an option list for users to select
MenubuttonMenu buttonIncluding drop-down and cascade
Listboxlist boxA list of options from which the user can select
Scaleprogress barThe start value and end value can be set, and the accurate value of the current position can be displayed
Scrollbarscroll barProvide scrolling function for its supported components (canvas, text box, text field, list box)
FrameframePure containers containing other components
ToplevelTop level containerSimilar to the framework, but provides a separate window container

4. Component properties: each component has its own properties and methods

Layout manager:

  • pack(): wrap the component into a parent component, and then create a layout
  • gird(): create a table like layout by organizing window components through a two-dimensional grid
  • place(): explicitly put a window component in the specified position

Some common properties:

  • Dimensions: length, width
  • Colors: colors
  • Fonts: fonts
  • Anchors: defines the relative position of the text
  • Relief styles: styles of components
  • Bitmaps: bitmaps
  • Cursors: cursors

5. Canvas components

  • A rectangular container in which graphics, text, components, or frames can be placed
  • Create canvas: canvas = Canvas(master,options=value,...)   
  • Parameter master: represents the parent window
  • Parameter options: sets the properties of the canvas
  • You can create and draw various graphics on the canvas
  • Common propertiesSpecific description
    bd

    The width of the border, in pixels. The default is 2

    bgbackground color
    confineWhen the default value is ture, the canvas has no scroll bar
    cursorThe shape of the cursor in the canvas, arrow, circle, dot
    heightheight
    widthwidth
    fillFill color
  • common methodSpecific description
    create_arc()Create arcs, chords, pie slices, simple arcs
    create_image()Create an image, bitmap image or an instance of a photo image class
    create_line()Create a line
    create_oval()Draw a circle or ellipse on the specified coordinates
    create_polygon()Draw a polygon (more than 3 vertices)
    create_rectangle()Draw a rectangle
#Draw a square on the canvas
from tkinter import *                     #Import ktinter module
tk = Tk()                                 #Create tkinter object
canvas = Canvas(tk,width=400,height=400)  #tk is the parent window and sets the length and width of the canvas
canvas.pack()                             #Layout manager
canvas.create_rectangle(50,50,100,100)    #Set the top left vertex coordinates and the bottom right vertex coordinates                                            
tk.mainloop()                             #Main event loop execution
#Draw a colored rectangle on the canvas
from tkinter import *                           #Import ktinter module
tk = Tk()                                       #Create tkinter object
canvas = Canvas(tk,width=400,height=400)        #tk is the parent window and sets the length and width of the canvas
canvas.pack()                                   #Layout manager
p1 = 10,50                                      #Set the vertex coordinates in advance and save them in the p1 variable
p2 = 300,100                                    #Set the vertex coordinates in advance and save them in p2 variable
canvas.create_rectangle(p1,p2,fill='yellow')    #The top left vertex is p1 and the bottom right vertex is p2
                                                #Fill sets the graphic fill color to yellow
tk.mainloop()                                   #Main event loop execution

#In this way, setting coordinates can be called repeatedly without rewriting
#Draw a triangle on the canvas
from tkinter import *                   #Import ktinter module
tk = Tk()                               #Create tkinter object
canvas = Canvas(tk,width=400,height=400)#tk is the parent window and sets the length and width of the canvas
canvas.pack()                           #Layout manager
p1 = 100,50                             #Set the vertex coordinates in advance and save them in the p1 variable
p2 = 150,100                            #Set the vertex coordinates in advance and save them in p2 variable
p3 = 50,100                             #Set the vertex coordinates in advance and save them in p3 variable
canvas.create_polygon(p1,p2,p3,fill='yellow',outline='red')
                                        #The coordinates of the three vertices of the triangle are P1, P2 and P3
                                        #Set the graphics fill color to yellow and the border color to red
tk.mainloop()                           #Main event loop execution
#Draw a polygon on the canvas
from tkinter import *                   #Import ktinter module
tk = Tk()                               #Create tkinter object
canvas = Canvas(tk,width=400,height=400)#tk is the parent window and sets the length and width of the canvas
canvas.pack()                           #Layout manager
p1 = 20,50                             #Set the vertex coordinates in advance and save them in p1 variable
p2 = 150,100                            #Set the vertex coordinates in advance and save them in the p2 variable
p3 = 200,100                             #Set the vertex coordinates in advance and save them in p3 variable
p4 = 100,300                             #Set the vertex coordinates in advance and save them in p3 variable
canvas.create_polygon(p1,p2,p3,p4,fill='blue',outline='red')
                                        #The coordinates of the four vertices of the polygon are p1,p2,p3 and p4
                                        #Set the drawing fill color to blue and the border color to red
tk.mainloop()                           #Main event loop execution
#Show text on canvas
from tkinter import *                   #Import ktinter module
tk = Tk()                               #Create tkinter object
canvas = Canvas(tk,width=400,height=400)#tk is the parent window and sets the length and width of the canvas
canvas.pack()                           #Layout manager
canvas.create_text(200,100,text='This is a test of canvas function',font=('Courier',12),fill='red')
                                        #200 and 100 are the display positions of text
                                        #Textparameter enter the displayed text
                                        #The font parameter sets the font and size, and the fill parameter sets the fill color to red
tk.mainloop()                           #Main event loop execution
#Draw various shapes on the canvas
#Let's take a look at create first_ Some setting parameters of arc()
#star parameter, which specifies the starting angle
#The EXT parameter specifies the offset of the angle
#Style parameter to specify the style


from tkinter import *                   #Import ktinter module
tk = Tk()                               #Create tkinter object
canvas = Canvas(tk,width=600,height=600)#tk is the parent window and sets the length and width of the canvas
canvas.pack()                           #Layout manager
p1 = 10,10,200,80                       #Set the coordinates of the upper left and lower right corners
p2 = 10,80,200,160                      #Set the coordinates of the upper left and lower right corners
p3 = 10,160,200,240                     #Set the coordinates of the upper left and lower right corners
p4 = 10,240,200,320                     #Set the coordinates of the upper left and lower right corners
p5 = 10,320,200,400                     #Set the coordinates of the upper left and lower right corners
canvas.create_arc(p1,star=0,extent=45,style=ARC)  #Draw an arc with a 45 degree offset and the style is arc
canvas.create_arc(p2,star=0,extent=90,style=ARC)  #Draw an arc with a 90 degree offset and the style is arc
canvas.create_arc(p3,star=0,extent=180,style=ARC) #Draw an arc with an offset of 180 degrees. The style is arc
canvas.create_arc(p4,star=0,extent=359,style=ARC) #Draw an arc with an offset of 359 degrees. The style is arc
canvas.create_arc(p5,star=0,extent=150,fill='blue')#Draw a sector with a 150 degree offset and a fill color of blue
tk.mainloop()                           #Main event loop execution
#Move triangle horizontally
from tkinter import *                   #Import ktinter module
import time                             #Import time module
tk = Tk()                               #Create tkinter object
canvas = Canvas(tk,width=800,height=800)#tk is the parent window and sets the length and width of the canvas
canvas.pack()                           #Layout manager
p1 = 100,50                             #Set the vertex coordinates in advance and save them in p1 variable
p2 = 150,100                            #Set the vertex coordinates in advance and save them in p2 variable
p3 = 50,100                             #Set the vertex coordinates in advance and save them in p3 variable
canvas.create_polygon(p1,p2,p3,fill='yellow',outline='red')
                                        #The coordinates of the three vertices of the triangle are P1, P2 and P3
                                        #Set the graphics fill color to yellow and the border color to red
#Code for moving triangles
for i in range(0,60):                  #60 times from 0 to 59, move the triangle every 0.05 seconds
    canvas.move(1,5,0)                 # 1 represents a triangle, moving 5 pixels horizontally and 0 pixels vertically
    tk.update()                        #Force the screen to update, constantly refresh the screen, so that we can see the graphics moving
    time.sleep(1)                     #Rest for 0.05 seconds before continuing the procedure
tk.mainloop()                          #Main event loop execution

#To be continued.....

Added by insrtsnhere13 on Fri, 21 Jan 2022 09:28:52 +0200