Python Basics 11 - Graphical Interface Programming

1. Pop-up message box

  To edit the message box, first import tkinter's messagebox, import method: from tkinter.messagebox import *

  ①A prompt message box pops up: showinfo(title='title', message='content')

  ②A warning message box pops up: showwarning(title='title', message='content')

  ③ An error message box pops up: showerror(title='title', message='content')

1 from tkinter.messagebox import *
2 showinfo(title='提示',message='welcome')
3 showwarning(title='提示',message='please input code')
4 showerror(title='提示',message='the code is false')

 

  ④A question message box pops up: askquestion(title='title', message='content'), the pop-up message box contains 'Yes' and 'No' buttons, and returns the corresponding string YES and NO values

   You can also use askesnocacncel(title='title', message='content') and askesno(title='title', message='content'), but these two return bool values, but the difference is that the former also has a cancel button, the latter does not

   Use askretrycancel(title='title', message='content') to pop up a question message box with buttons of 'retry' and 'cancel', returning a bool value

1 ret=askyesnocancel(title='Are you sure?',message='weclome')
2 if ret==True:
3     showinfo(title='提示',message='welcome')
4 elif ret==False:
5     showinfo(title='提示',message='False')
6 else:
7     showinfo(title='提示',message='cancel ' )
1 ret=askyesno(title='Are you sure?',message='weclome')
2 if ret==True:
3     showinfo(title='提示',message='welcome')
4 if ret==False:
5     showinfo(title='提示',message='False')
1 ret=askretrycancel(title='Are you sure?',message='weclome')
2 if ret==True:
3     showinfo(title='提示',message='welcome')
4 if ret==False:
5     showinfo(title='提示',message='False')

2. Create a Windows window

  ①Import the Tkinter module: from tkinter import *

  ②Create a window: window object = Tk()

  ③Display window: window object.mainloop()

  ④Change the title: window object.title('title')

  ⑤Set the initial size of the window: window object.geometry('size'), parameter size: width x height (the middle is the letter x, not the multiplication sign)

   Set the minimum size of the window: window object.minsize('size'); set the maximum size of the window: window object.maxsize('size'); parameter size: width, height

1 from tkinter import *
2 win=Tk()
3 win.title('mywindows')
4 win.geometry('800x600')
5 win.minsize(400,300)
6 win.maxsize(1440,900)
7 win.mainloop()

3. Label component

  ①The label component is mainly used to display text or attempts in the window. The creation method is:

    label object = Label (main window, text = the text displayed by the label component)

    The display method only needs to mobilize the pack() method

  ②Display picture: Use the bitmap attribute to display the bitmap in the label component. The value of the bitmap is as follows:

value specific description
error error icon
hourglass Hourglass icon
info information icon
questhead Question avatar icon
question question icon
warning warning icon
gray12 Grayscale background icons
gray25 Grayscale background icons
gray50 Grayscale background icons
gray75 Grayscale background icons

  ③Custom image: use image and bm attributes

  ④Set the front and background colors: use the fg attribute to set the foreground color, the bg attribute to set the background color, and the value of bg is the uppercase string of the color word

  ⑤Other common attributes:

Attributes specific description
width width
height high
compound

 Specifies how text and images are displayed on the label. The default value is Nnone. When image/bitmap is specified, the text will be overwritten. The values ​​are as follows

left: the image is on the left; right: the image is on the right; top: the image is on the top; bottom: the image is on the bottom; center: the text is overlaid on the image

waplength Specify how many units to start wrapping 
justify Specify the alignment of multiple lines, you can use LEFT or RIGHT
ahchor

Specifies the text or image in the label

e, vertical center, horizontal right

w, vertical center, horizontal left

n, vertical top, horizontal center

s, vertical bottom, horizontal center

ne, vertical top, horizontal right

se, vertical bottom, horizontal right

sw, vertical bottom, horizontal left

nw, vertical top, horizontal left

center is centered vertically, centered horizontally

 1 from tkinter import*
 2 win=Tk()
 3 win.title('mywindows')
 4 lb=Label(win,bitmap='error')
 5 lb.pack()
 6 bm=PhotoImage(file='C:\\Users\\cai\\Desktop\\tp.png')
 7 lb2=Label(win,image=bm)
 8 lb2.bm=bm
 9 lb2.pack()
10 lb3=Label(win,fg='RED',bg='BLUE',text='color')
11 lb3.pack()
12 win.mainloop()

四、Button组件

  ①用于在窗口中显示按钮,按钮上可以显示文字或者图像,创建语法如下;

    Button 对象=Button(窗口对象,text=‘ ’,command=‘单击按钮所调用的’)

  ②属性

属性 具体说明
image、bm 自定义Button显示图片
height 高度
width 宽度
bitmap 指定按钮上个显示的位图
bd 设置按钮边框大小
wraplength 指定多少单位后换行,用于多行显示文本
bg 背景色
fg 前景色
state 设置组件状态,取值:NORMAL(正常),ACTIVE(激活),DISABLED(禁用)
compound 与label一样

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325302585&siteId=291194637