Use of Tkinter in Python (appropriate event collation) (ii)

Source: Internet
Author: User

1, fonts (font)

General format: (' Times-10 bold ')

(' Times ', ten, ' bold ', ' italic ') in turn, indicate font, font size, bold, italic

2, the use of pictures (image)

Photo=photoimage (file= ' path.gif ')

Canvas = Canvas.create_image (Image=photo)

Tkinter only supports a few other formats, such as GIF and BMP, you want to insert other format pictures you need to import Pil;pip install a Third-party class library pillow

From PIL import image,imagetk

Source=image.open (' path.jpg ')

Photo=imagetk.photoimage (source)

3. Data Container

Using the set and get methods to pass values and values,

Stringvar (): Character type

Booleanvar (): Boolean type

Intvar (): Integer type

Doublevar (): Floating-point type

4. Data binding: (Transfer value)

Entry, Text, Laber, Button,Radiobutton, Checkbutton: using textvariable to receive or display variable text

Entryvalue = Stringvar ()

Entry = entry (Textvariable=entryvalue) entryvalue.get () #输出接收的值

5. Data binding: (Association)

Radiobutton,checkbutton: Using value (values of component association variables), variable (variables associated with the component)

Rbvalue = Intvar ()

RB1 = Radiobutton (text= "Red", Variable=rbvalue,value=1,command=func)

6. Bind event bind (sequence,func,add) bind_class (classname,sequence,func,add) bind_all (sequence, Func,add)

For a button component, a menu component, and so on, you can specify its event handler function by using the command parameter when you create a component. method is bind, or the Bind_class method is used to bind the class, and the Bind_all method binds all component events to the event response function.

Event Parameters:

Event Parameters: Sequence The event that is bound;
Func The event handler function that is bound, that is, the response event;
Add Optional parameter, null character or ' + ';
ClassName The class that is bound;

7, mouse keyboard events

<Button-1> left mouse button press, 2 for the middle key, 3 for the right key;

<ButtonPress-1> ditto;

<ButtonRelease-1> left mouse button release;

<B1-Motion> Press and hold the left mouse button to move;

<Double-Button-1> double click left;

<Enter> mouse pointer into a component area;

<Leave> mouse pointer leaves a component area;

<MouseWheel> rolling wheel;

<KeyPress-A> Press 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> Quick Press A two;

<Lock-KeyPress-A> in capital State press A;

<Key> refers to any keyboard keys,

8. Window Events

Activate triggers when the component is converted from unavailable to available;

Configure is triggered when the size of the component changes;

Deactivate triggers when the component is converted from usable to unavailable;

Destroy is triggered when the component is destroyed;

The expose is triggered when the component is exposed from the occluded state;

Unmap triggers when the component is changed from a display state to a hidden state;

MAP triggers when the component changes from a hidden state to a display state;

Focusin triggers when the component gets focus;

Focusout triggers when the component loses focus;

Property is triggered when a form's properties are deleted or changed;

Visibility triggers when the component becomes visible;

9. Responding to Events

The API for the event object (Def func (event)):

Char Returns the key character, valid only for keyboard events;
KeyCode Returns the key encoding, which is only valid for keyboard events;
Keysym Returns the key name, valid only for keyboard events;
Num mouse button, only the mouse event is valid;
Type The type of event that is triggered;
Widgets The component that caused the event;
Width,heigh The size of the component changed, only configure valid;
X,y Returns the current position of the mouse, relative to the window;
X_root,y_root Returns the current position of the mouse, relative to the entire screen

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.