First, pop-up message box
To edit a message box, first import the Tkinter MessageBox, import method: from Tkinter.messagebox Import *
① Popup prompt message box: showinfo (title= ' title ', message= ' content ')
② pop-up warning message box: showwarning (title= ' title ', message= ' content ')
③ Pop-up error message box: ShowError (title= ' title ', message= ' content ')
1 fromTkinter.messageboxImport*2Showinfo (title='Tips', message='Welcome')3Showwarning (title='Tips', message='Please input code')4ShowError (title='Tips', message='The code is false')
④ pop-up question message box: askquestion (title= ' title ', message= ' content '), pop-up message box containing ' yes ' and ' no ' button, return the corresponding string yes and no value
You can also use Askyesnocacncel (title= ' title ', message= ' content ') and AskYesNo (title= ' title ', message= ' content '), but these two return bool values, But the difference is that the former has a Cancel button, which has no
Use Askretrycancel (title= ' title ', message= ' content ') to eject the question message box with ' Retry ' and ' Cancel ' button, return bool value
1Ret=askyesnocancel (title='Is you sure?', message='Weclome')2 ifret==True:3Showinfo (title='Tips', message='Welcome')4 elifret==False:5Showinfo (title='Tips', message='False')6 Else:7Showinfo (title='Tips', message='Cancel')
1Ret=askyesno (title='Is you sure?', message='Weclome')2 ifret==True:3Showinfo (title='Tips', message='Welcome')4 ifret==False:5Showinfo (title='Tips', message='False')
1Ret=askretrycancel (title='Is you sure?', message='Weclome')2 ifret==True:3Showinfo (title='Tips', message='Welcome')4 ifret==False:5Showinfo (title='Tips', message='False')
Second, create Windows window
① Import tkinter module: from tkinter Import *
② Create window: Window Object =TK ()
③ Display window: Window object. Mainloop ()
④ Change title: Window object. Title (' title ')
⑤ Set window Initial Size: Window object. Geometry (' size '), parameter size: width x Height (middle is letter X, not multiplication sign)
Set window Minimum Size: Window object. minsize (' size '); Set window Maximum size: Window object. maxsize (' size '); parameter size: width, height
1 from 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 ()
Third, label components
The ①label component is primarily used to display text in a window or to attempt to create a method that:
Label Object =label (the main window, the text displayed by the Text=label component)
Display method you can only transfer the pack () method
② Display Picture: Use bitmap to display bitmaps in a label component, the value of bitmap is the following table:
| 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 Pictures: Use the image and BM properties to
④ setting the front and back colors: Setting the foreground color with the FG property, setting the background colour for the BG property, and the uppercase string for a color word in BG
⑤ Other Common properties:
| Property |
Specific description |
| Width |
Width |
| Height |
Height |
| Compound |
Specifies how text and images display the default value of Nnone on a label, and the text overrides the value when IMAGE/BITMAP is specified as follows
Left: The image is right: the image of the station; top: image on top; bottom: image in; Center: text Overlay on image |
| Waplength |
Specify how many units to start a line break |
| Justify |
To 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, horizontally centered
s, vertical, horizontally centered
NE, vertical, horizontal right.
SE, vertical, horizontal right
SW, vertical, Horizontal left
NW, vertical top, horizontal left
Center centered vertically, horizontally centered |
1 fromTkinterImport*2win=Tk ()3Win.title ('mywindows')4Lb=label (win,bitmap='Error')5 Lb.pack ()6Bm=photoimage (file='C:\\users\\cai\\desktop\\tp.png')7Lb2=label (win,image=BM)8lb2.bm=BM9 Lb2.pack ()TenLb3=label (win,fg='RED', bg='BLUE', text='Color') One Lb3.pack () AWin.mainloop ()
Four, button components
① is used to display the button in the window, the button can display text or images, creating the following syntax;
Button Object =button (Window object, text= ', command= ' click button called ')
② Property
| Property |
Specific instructions |
| Image, BM |
Customize button Display Picture |
| Height |
Height |
| Width |
Width |
| Bitmap |
Specifies the bitmap that the button displays last |
| Bd |
Set button border size |
| Wraplength |
Specify how many units to wrap to display text in multiple lines |
| Bg |
Background color |
| Fg |
Front view |
| State |
Set component Status, value: Normal (normal), active (Active), DISABLED (disabled) |
| Compound |
As with label |
Python basic 11-graphical interface programming