This article originates from https://www.cnblogs.com/aland-1415/p/6849193.html
1, use Tkinter. Tk () generates the main window (Root=tkinter. Tk ());
Root.title (' title name ') modifies the name of the box body, or it can be named with the classname parameter when it is created;
Root.resizable (0,0) frame size adjustable, respectively, to indicate the variability of x, y direction;
Root.geometry (' 250x150 ') specifies the size of the main frame body;
Root.quit () exit;
Root.update_idletasks ()
Root.update () refresh the page;
2. Primary Sample:
ImportTkinterroot=tkinter. Tk ()#Generate root main windowLabel=tkinter. Label (root,text='Hello,gui')#Generate LabelsLabel.pack ()#add a label to the main windowButton1=tkinter. Button (root,text='Button1')#Generate button1Button1.pack (Side=tkinter. left)#Add button1 to the root main windowButton2=tkinter. Button (root,text='Button2') Button2.pack (Side=Tkinter. right) Root.mainloop ()#Enter message loop (required component)
3.15 Core components in Tkinter:
button buttons; a Canvas drawing component in which you can draw graphics; Checkbutton check box; Entry text box (single line); Text box (multiple lines); frame frame, a set of several components to form a label label, you can display text or pictures; listbox list box; Menu menu; Menubutton Its functions can be replaced with the menu , the Message is similar to the label component, but it can be wrapped according to its own size; Radiobutton single box; scale slider; allows you to set a numeric value by the slider Scrollbar and The standard scroll bar of the text widget; toplevel is used to create a subwindow window component. (In Tkinter, the window part class has no rating; all widgets are brothers in the tree.) )
4. Placement and layout of components (Pack,grid,place)
Pack Component Set Location property parameter: After: Place component after other component; Before: Before placing component in other components; Anchor: Alignment of components, top alignment'N', bottom-aligned's'Left'W'Right'e'side: The position of the component in the main window, which can be'Top','Bottom',' Left',' Right'(Tkinter when used.) Top,tkinter. E); Fill fill mode (Y, vertical, X, horizontal) Expand 1 extensible, 0 non-extensible grid components use the row and column method to place the component's position, with the Parameters: column: The beginning of the columns where the component is located Columnspam: The column width of the component; row: The starting position of the line where the component is located; Rowspam: The line width of the component; The place component can use coordinates to position the component directly, with the parameters: Anchor: component alignment; x: The x-coordinate of the upper-left corner of the component; y: the y-coordinate of the upper-right corner of the component; Relx: The x-coordinate of the component relative to the window should be 0-decimal between 1; rely: The component should be 0 relative to the y-coordinate of the window.-1 decimal; width: The width of the component; HEITHT: The height of the component; Relwidth: The width of the component relative to the window, 0-1; Relheight: The height of the component relative to the window, 0-1;
5, use Tkinter. Control button Parameters when button:
Anchor: Specifies the position of the text on the button, background (BG) Specifies the background color of the button, bitmap: Specifies the bitmap to display on the button; borderwidth (BD) Specifies the width of the button border; command: Specifies the callback function for the button message; cursor: Specifies the pointer style that the mouse moves to the button; font: Specifies the font of the text on the button, foreground (FG) specifies the foreground color of the button, height : Specifies the height of the button; Image: Specifies the picture displayed on the button ; state: specifies the state of the button (disabled); text : Specifies the width of the button that is displayed on the button padx Set Text and button border X The distance, there are pady; Activeforeground press the foreground color textvariable variable text, with Stringvar and so on with
6, text box Tkinter. Entry,tkinter. Text Control parameters:
background (BG) text box background color; foreground (FG) foreground color; selectbackground selected text background color ; Selectforeground selected text foreground color; borderwidth (BD) text box border width; font font; show The character displayed by the text box, if *, indicates that the text box is a password box; state status; width text box textvariable variable text, used in conjunction with STRINGVAR, etc.
7, label Tkinter. Label Component Control Parameters:
Anchor The position of the text in the label, background (BG) background color, foreground (FG) foreground color, borderwidth (BD) border width; width label height; the bitmap in the bitmap tag; font font; Image Label justify the alignment of multiple lines of text; text in the textbox can use '\ n' indicates that the newline textvariable display text is automatically updated, with the stringvar, etc.
8, Radio box and check box Radiobutton,checkbutton control parameters:
Anchor text position, background (BG) background color, foreground (FG) foreground color, borderwidth border width; width The width of the component, height of the component, bitmap in the bitmap component, picture in the image component; a font font; The alignment of Multiple lines of text in a justify component; The text of the specified component; value Specifies the value of the associated variable in the selected component, variable the variable associated with the specified component , and Indicatoron Special control parameter, when 0 o'clock, the component is drawn as a button; Textvariable variable text display, with Stringvar, etc.
9. Group Canvas Control parameters
background (BG) background color; Foreground (FG) foreground color; BorderWidth the width of the component border, width, height; Bitmap bitmap; Image Image; The main methods of drawing are as follows: Create_arc arc; Create_bitmap draw bitmap, support XBM; Create_image draw picture, support GIF (x,y,image,anchor); Create_line draw spur line; Create_oval; Draw the ellipse, Create_polygon draw the polygon (coordinates in order, without parentheses, and parameters, Fill,outline); Create_rectangle Draws a rectangle ((a,b,c,d) with the upper-left and lower-right coordinates); Create_text Draw text (font parameter fonts,); Create_window Draw the window; Delete Deletes the drawing; Itemconfig Modify Graphic properties, the first parameter is the ID of the graphic, and the parameter that you want to modify is behind it; move moves the image (1,4, 0), 1 is an Image object, 4 is a traverse 4 pixels, 0 is a vertically shifted pixel, and then root.update () refreshes to see the movement of the image, in order to make multiple movements visible, preferably with the time.sleep () function; As long as the Create_ method to draw a graph, It automatically returns an ID, which is assigned to a variable when a drawing is created, and can be used when the ID is required. The coords (ID) returns the two coordinates of the object's position (4 numeric tuples), and for a button component, a menu component, and so on, you can specify its event handler function through the command parameter when the component is created. method is bind, or the Bind_class method is used for class binding, and the Bind_all method binds all component events to the event response function.
10. Menu
Parameters: tearoff , 0 for the original window, 1 for the click divided into two windows BG,FG background, foreground borderwidth border width font Font activebackgound when clicked background, same activeforeground,activeborderwidth,disabledforeground cursor Postcommand selectcolor selected when background takefocus title Type Relief method: menu.add _cascade Add a sub -option menu.add_command add a command (the label parameter is displayed) menu.add_separator add a divider line Menu.add_checkbutton Add Acknowledgement button Delete Delete
11. Event Correlation
bind (Sequence,func,add)--bind_class (classname,sequence,func,add) bind_all (sequence,func,add) event arguments: sequence The event handler bound by the Func, the Add optional argument, the null character, or the '+'; ClassName the class that is bound; Mouse Keyboard event<Button-1>The left mouse button press, 2 means the middle key, 3 means right button;<ButtonPress-1>Ibid.<ButtonRelease-1>left mouse button release;<B1-Motion>Press and hold the left mouse button to move;<Double-Button-1>double left button;<Enter>The mouse pointer enters a component area;<Leave>The mouse pointer leaves a component area;<MouseWheel>rolling wheel;<KeyPress-A>Press the A key, a can be replaced by other keys;<Alt-KeyPress-A>Press ALT and A;alt at the same time to use CTRL and Shift instead;<Double-KeyPress-A>Press twice a quickly;<Lock-KeyPress-A>Press A; window event Activate When the component is unavailable to be available, Configure when the component size changes; Deactivate Triggered when a component is converted from available to unavailable, Destroy when the component is destroyed, Expose when the component is exposed from the occlusion state; Unmap when the component is Triggered when the display state becomes hidden; the Map is triggered when the component is changed from a hidden state to a display state; Focusin triggers when the component receives focus; focusout when the component loses The property is triggered when the properties of the form are deleted or changed, Visibility when the component becomes visible, and the Response event object (deffunction (Event)): Char key character, only valid for keyboard events, KeyCode key name, only for keyboard events; Keysym key Only valid for keyboard events; num mouse buttons are only valid for mouse events; type triggers event types; The widget causes the event's component; wid Th,heigh component changed size, only configure valid; X, y mouse current position, relative to window, x_root,y_root mouse current position, relative to entire screen
12. Pop-up window
Control parameters of the Messagebox._show function: default Specifies a message box button; icon specifies a message box; message Specifies the messages that the message box displays; the parent of the specified message box; title title; type; simpledialog module parameter: The caption of the specified dialog box; prompt displayed text; initialvalue Specifies the initial value of the input box; FileDialog module parameter: filetype Specifies the file type; Initialdir Specifies the default directory; initialfile specifies the default file; title specifies the dialog title Colorchooser module parameters: Initialcolor Specifies the initialization color; title Specifies the title of the dialog box;
13. Fonts (font)
General format:
(' Times-10 bold ')
(' Times ', ' ten ', ' bold ', ' italic ') in turn indicate font, font size, bold, italic
Add:
Config re-configuration
Label.config (font= ' Arial-%d bold '% Scale.get ())
Font, size (size can be font size), bold
Tkinter. Stringvar can automatically refresh the string variables, the use of set and get methods for value and value, similar to Intvar,doublevar ...
Tkinter Module Common parameters (Python3)