Tkinter Module Common parameters (Python3)

Source: Internet
Author: User

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:
Import Tkinter
Root=tkinter. Tk () #生成root主窗口
Label=tkinter. Label (root,text= ' Hello,gui ') #生成标签
Label.pack () #将标签添加到主窗口
Button1=tkinter. Button (root,text= ' Button1 ') #生成button1
Button1.pack (Side=tkinter. left) #将button1添加到root主窗口
Button2=tkinter. Button (root,text= ' Button2 ')
Button2.pack (Side=tkinter. right)
Root.mainloop () #进入消息循环 (required component)

3.15 Core components in Tkinter:
button buttons;
A canvas drawing component in which graphics can be drawn;
Checkbutton check box;
Entry text box (single line);
Text textbox (multiple lines);
Frame frame, which sets up a set of several components
A label tag that can display text or pictures;
Lisbox list box;
menu menus;
Menubutton its functions can be completely replaced with the menu;
The Message is similar to the label component, but can wrap text according to its size;
Radiobutton Radio Box;
Scale slider, allowing a numeric value to be set by the slider
Scrollbar scroll bar; Use the standard scroll bar of the canvas, entry, ListBox, and text widgets;
The 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)
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 assembly, top-aligned ' n ', bottom-aligned ' s ', left ' W ', right ' e '
Side: The position of the component in the main window can be ' top ', ' bottom ', ' left ', ' right ' (tkinter when used.) Top,tkinter. E);
Fill Filling method (Y, vertical, X, horizontal)
Expand 1 Extensible, 0 non-extensible
The grid component uses the row and column method to place the component's position, with the parameters:
Column: The starting position 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 components directly, with parameters such as:
Anchor: alignment of components;
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, which should be a decimal between 0-1;
Rely: The component's y-coordinate relative to the window, which should be a decimal between 0-1;
Width: The widths of the components;
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 displayed on the button;
BorderWidth (BD): Specifies the width of the button border;
Command: Specifies the callback function of the button message;
Cursor: Specifies the pointer style to move the mouse over the button;
Font: Specifies the typeface of the text on the button;
Foreground (FG): Specifies the foreground color of the button;
Height: Specifies the level of the button;
Image: Specifies the picture displayed on the button;
State: Specifies the status of the button (disabled);
Text: Specifies that the button is displayed;
Width: Specify a button's widths
PADX sets the distance between the text and the button border X, as well as the pady;
Activeforeground the foreground color when pressed
Textvariable variable text, used in conjunction with STRINGVAR, etc.


6 , text box Tkinter. Entry,tkinter. Text control parameters:
    background (BG)     text box background color;
     Foreground (FG)       foreground,
    selectbackground selected text background color;
     selectforeground selected text foreground color,
    borderwidth (BD)     text box border width;
     font                fonts;
    show              text box display characters, if * , which indicates that the text box is a password box;
    state             
    width        text box width
    textvariable    variable text, with Stringvar, etc. with the


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 widths;
Height label altitude;
Bitmap the bitmap in the tag;
font fonts;
The image in the image tag;
Justify the alignment of multiple lines of text;
Text in the textbox, you can use ' \ n ' to indicate a line break
Textvariable display text automatically update, with Stringvar, etc. 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 of the component;
Height of the component;
The bitmap in the bitmap component;
Image in the image component;
font fonts;
The alignment of multiple lines of text in a justify component;
Text of the specified component of text;
value specifies the values of the associated variables in which the component is selected;
Variable the variables associated with the specified component;
Indicatoron Special control parameters, when 0 o'clock, the component will be drawn as a button form;
Textvariable variable text display, with Stringvar, etc.


9. Group Canvas control Parameters:
Background (BG) background color;
Foreground (FG) foreground color;
BorderWidth component Border width;
width of the component;
Height altitude;
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 the spur line;
Create_oval; Draw an ellipse;
Create_polygon Draw polygons (coordinates are listed in order, without parentheses, and parameters, Fill,outline);
Create_rectangle Draws the rectangle ((a,b,c,d), the value is the upper-left corner and the lower-right corner of the coordinates);
Create_text drawing text (font parameter fonts,);
Create_window drawing window;
Delete Deletes the drawn graphic;
Itemconfig Modify the graphic properties, the first parameter is the ID of the graph, and the parameter that you want to modify is behind;
Move Moving Image (1,4,0), 1 is an Image object, 4 is the Traverse 4 pixels, 0 is a longitudinal pixel, and then with Root.update () refresh can see the movement of the image, in order to make multiple movements become visible, preferably with time.sleep () function;
As long as you draw a graph with the Create_ method, you automatically return an ID, assign it to a variable when creating a graph, and use the variable name when you need an ID.
Coords (ID) returns the two coordinates (4 numeric tuples) of the position of the object;

For button components, menu components, and so on, you can specify their event handlers through the command parameter when you create a component. 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 Sub-window, 0 for the original window, 1 for the click divided into two windows
BG,FG background, foreground
BorderWidth Border Width
Font fonts
Activebackgound the background when clicked, also has Activeforeground,activeborderwidth,disabledforeground
Cursor
Postcommand
Selectcolor Background When selected
TakeFocus
Title
Type
Relief

Method:
Menu.add_cascade adding sub-options
Menu.add_command Add a command (the label parameter is the display content)
Menu.add_separator Adding a separator line
Menu.add_checkbutton Add Confirmation button
Delete Deletes


11. Event Correlation
Bind (Sequence,func,add)--
Bind_class (Classname,sequence,func,add)
Bind_all (Sequence,func,add)
Event Parameters:
The event that the sequence binds;
The event handler function that is bound by Func;
Add optional parameters, either null characters or ' + ';
The class that the className binds to;

Mouse keyboard Events
<Button-1> left mouse button press, 2 means the middle key, 3 means right button;
<ButtonPress-1> Ibid.
<ButtonRelease-1> left mouse button release;
<B1-Motion> hold down the left mouse button to move;
<Double-Button-1> double left button;
<Enter> mouse pointer into a component area;
<Leave> mouse pointer away from 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 in the uppercase State;

Window events
Activate triggered when the component is unavailable to be available;
Configure triggers when the component size changes;
Deactivate triggers when a component is changed from available to unavailable;
Destroy triggered when the component is destroyed;
Expose is triggered when the component is exposed from the occluded state;
Unmap triggered when a component is changed from a display state to a hidden state;
MAP triggers when the component is changed from hidden to display state;
Focusin triggers when the component receives focus;
Focusout triggers when the component loses focus;
Property is triggered when the properties of a form are deleted or changed;
Visibility is triggered when the component becomes visible;

Response Event
Event object (Def function):
    char           key characters, only valid for keyboard events,
    keycode    key names, only valid for keyboard events;
    Keysym      Key encoding, only valid for keyboard events;
    num          mouse buttons, Valid for mouse events only;
    type          event types triggered;
     widget       the component that caused the event;
    Width,heigh the size of the component after the change, only configure valid;
     x,y          The current position of the mouse relative to the window;
    x_root,y_ Root mouse current position, relative to the entire screen

12 , pop-up window
messagebox._show function control parameters:
    default         Specify a message box button;
    icon             Specify a message box icon;
    message     Specifies the message that the message box displays;
    parent        the parent component of the specified message box;
    title             title;
    type             type;

Simpledialog Module Parameters:
TITLE Specifies the caption of the dialog box;
Prompt the text displayed;
INITIALVALUE Specifies the initial value of the input box;

FileDialog Module Parameters:
filetype specifies the file type;
INITIALDIR specifies the default directory;
INITIALFILE specifies the default file;
TITLE Specifies the caption of the dialog box

Colorchooser Module Parameters:
INITIALCOLOR specifies the initialization color;
TITLE Specifies the caption 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.