Transferred 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:
1 Import tkinter2 Root=tkinter. Tk () #生成root主窗口3 label=tkinter. Label (root,text= ' Hello,gui ') #生成标签4 label.pack () #将标签添加到主窗口5 button1=tkinter. Button (root,text= ' Button1 ') #生成button16 button1.pack (side=tkinter. left) #将button1添加到root主窗口7 Button2=tkinter. Button (root,text= ' Button2 ') 8 Button2.pack (Side=tkinter. right) 9 Root.mainloop () #进入消息循环 (required component)
3.15 Core components in Tkinter:
Press CTRL + C to copy the code<textarea></textarea>Press CTRL + C to copy the code
4. Placement and layout of components (Pack,grid,place)
The pack component sets the Location property parameter: after: The component is placed after another component, before: before the component is placed in another component , Anchor: Alignment of the component, top-aligned ' n ', Bottom-aligned ' s ', left ' W ', and Right ' E ' side: The component's position in the main window, can be ' top ', ' bottom ', ' ieft ', ' R ' (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 starting position of the column where the component is located; Columnspam: Column width of the component ; row: The starting position of the row where the component is located; rowspam: line width of the component Place components can be placed directly using coordinates, parameters are: 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 component's x-coordinate relative to the window, which should be a decimal between 0-1, rely: The component's y-coordinate relative to the window, a decimal between 0-1, and width : the component's breadth; HEITHT: The height of the component; relwidth: The component is relative to the width of 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 Select text foreground color , BorderWidth (BD) text box border width, font font , show text box display character, if *, indicates text box is password box; state status; width text box textvariable variable text, 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 width; height tag altitude; bitmap in bitmap tag; font font; image A picture in a label; justify the alignment of multiple lines of text ; text in a TextBox that can use ' \ n ' to represent a newline textvariable Display text automatically updates, with Stringvar and so on with
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, the parameter that you want to modify, the move Moving Image (1,4,0), 1 for the Image object, 4 for the 4 pixels, 0 for the vertical shift of the pixel, and then the Root.update () refresh to see the image move, in order to make multiple moves become View, preferably with time.sleep () function, as long as the Create_ method to draw a graph, it will automatically return an ID, create a graph when you assign it to a variable, you need an ID to use this variable name. 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 tied The event handler that is bound by the Func, the Add optional argument, the null character or ' + ', the class that the ClassName is bound to, and the mouse keyboard event <butt On-1> the left mouse button press, 2 for the middle key, 3 for the right key; <ButtonPress-1> Ibid; <ButtonRelease-1> left mouse button release; <b 1-motion> hold down the left mouse button to move; <Double-Button-1> double-click; <Enter> mouse pointer into a component area; < Leave> the mouse pointer away from a component area; <MouseWheel> scroll wheel; <KeyPress-A> Press the A key, a can be substituted by other keys; <Alt-KeyPress-A> Press ALT and A;alt at the same time to replace with CTRL and shift; <Double-KeyPress-A> quickly press two A; <lock-keypress-a> ; Press A; Window event Activate When the component is unavailable to be available, Configure when the component size changes; Deactivate when the component is changed by the available Destroy triggers when the component is destroyed, Expose when the component is exposed from the occlusion state; Unmap when the component becomes hidden from the display state When the state is triggered; Map Triggered when the component is changed from a hidden state to a display state, focusin when the component receives focus, focusout when the component loses focus; Triggered when the properties of the form are deleted or changed, Visibility when the component becomes visible, Response event object (Def function): Char key character, only for keyboard events are valid; KeyCode key names, only valid for keyboard events; Keysym key code, only valid for keyboard events; num mouse button is only valid for mouse events; Ype the type of event triggered by the widget, the component that caused the event, the size of the Width,heigh component after the change, only configure valid; The current position of the mouse relative to the window; X_root,y_root the current position of the mouse relative to the 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 ...
Color in the attached 1:tkinter
Python tkinter Modules and parameters