Tkinter learning-event binding and window, tkinter window

Source: Internet
Author: User

Tkinter learning-event binding and window, tkinter window
Reading directory:

  • Event binding

  • Toplevel component
  • Standard dialog box

Event binding:

Note: For each component, bind () can be used to bind a function or method to a specific event.

Event Sequence:

Note: You need to use the bind () method to bind a specific event sequence to a custom method. The time sequence is represented in strings.

Syntax description:

<Modifier-type-dateil>

The event sequence must be included in angle brackets (<...> ).

The content in the "type" section is the most important. It is usually used to describe common data types, such as mouse clicking or keyboard clicking (<Button-1>, indicating that the user clicks the left mouse Button)

The modifier part is optional. It is usually used to describe the Key combination, for example, Ctrl + c, (<Key-H>, indicating that the user presses H)

The dateil part is optional. It is usually used to describe the specific Key (<Control-Shift-Key-H>, indicating that the user simultaneously presses Ctrl + Shift + H)

Type (commonly used ):

Button: this event is triggered when you click the mouse Button. <Button-1> left-click, <Button-2> middle-click, <Button-3> right-click, <Button-4> scroll wheel (liunx), <Button-5> scroll wheel (liunx)

ButtonRelease: The event triggered when the user releases the mouse button.

KeyPress: this event is triggered when the user presses the keyboard, abbreviated as key

Modifier (commonly used ):

Alt: When you press the Alt button

Any: indicates that Any type of buttons are pressed. For example, <Any-KeyPress> indicates that when a user presses Any key

Control: When the Ctrl key is pressed

Double: When two subsequent events are triggered consecutively, for example, <Double-Button-1>, Double-click the left mouse Button.

Lock: When uppercase letters are opened

Shift: When you press Shift

Triple: When three subsequent events are triggered

Event object:

Note: When Tkinter calls back a predefined function, it will call it with an Evnet object. Commonly Used functions include:

X, y coordinates of the current mouse position (relative to the upper left corner of the window)

X_root, y_root coordinates of the current mouse position (relative to the upper left corner of the screen)

Character corresponding to the char key

Keysym key name

Keycode key code

Get the cursor position:

From tkinter import * root = Tk () frame = Frame (root, width = 200, height = 200) def callback (event): print ('current position: ', event. x, event. y) frame. bind ('<Button-1>', callback) frame. pack () mainloop ()

 

Result:

    

Accept Keyboard Events. Only the component obtains the focus can receive Keyboard Events. Use focus_set () to obtain the focus.

From tkinter import * root = Tk () def callback (event): print ('position: ', repr (event. char) frame = Frame (root, width = 200, height = 200) frame. bind ('<Key>', callback) frame. focus_set () frame. pack () mainloop ()

 

Result:

    

Obtains the Motion Track of the mouse, using <Motion>

From tkinter import * root = Tk () def callback (event): print ('current location: ', event. x, event. y) frame = Frame (root, width = 600, height = 200) frame. bind ('<Motion>', callback) frame. pack () mainloop ()

 

Result:

    

Toplevel:

Note: The top-level window is used to display additional windows and other pop-up windows.

From tkinter import * root = Tk () def show (): top = Toplevel () top. title ('mountain ') top. geometry ('200x200') Label (top, text = 'cross Mountain '). pack () Button (root, text = 'create top-level Windows', command = show ). pack () mainloop ()

Result:

      

The attributes method is used to set window properties.

From tkinter import * root = Tk () def show (): top = Toplevel () top. title ('mountain ') top. geometry ('200x200') top. attributes ('-alpha', 0.5) # Set the window transparency to 50% Label (top, text = 'cross Mountain '). pack () Button (root, text = 'create top-level Windows', command = show ). pack () mainloop ()

 

Result:

    

Standard dialog box:

Note: There are three modules: messagebox (message dialog box), filedialog (file dialog box), and colorchooser (color dialog box)

Message dialog box:

There are seven styles in the following format: showwarning (title, message, options)

Title: Set the title bar text

Message: set text content

Options: Set the meaning of the option

Return Value:

Askokcancel (), askretrycancel (), askyesno () returns a Boolean value.

Askquestion () returns 'yes' or 'no'

Showerror (), showinfo (), showwarning () returns 'OK', indicating that the user clicked 'OK'

From tkinter import * from tkinter import messageboxroot = Tk () def show (): # messagebox. askokcancel ('mountain ', 'askokcancel') # messagebox. askquestion ('mountain ', 'askquestion') # messagebox. askretrycancel ('mountain ', 'askretrycancel') # messagebox. askyesno ('mountain ', 'askyesno') # messagebox. showerror ('mountain ', 'showerror') # messagebox. showinfo ('mountain ', 'showinfo') messagebox. showwarning ('mountain ', 'showwarning') Button (root, text = 'open', command = show ). pack () mainloop ()

Result:

              

File Dialog Box:

Two functions askopenfilename () open the file, asksaveasfilename () save the file

Common values:

Initialdir: Specifies the path to open or save

Return Value:

If you select a file, the full path of the file is returned.

If the cancel button is selected, an empty string is returned.

From tkinter import * from tkinter import filedialogroot = Tk () def show (): filedialog. askopenfilename (initialdir = r 'pathname') Button (root, text = 'open file', command = show ). pack () mainloop ()

Color dialog box:

Return Value:

If you select a color and click OK, a binary group is returned. The first element is the RGB color value, and the second is the hexadecimal color value of the corresponding element.

If you click Cancel, the returned result is (None, None)

From tkinter import * from tkinter import colorchooserroot = Tk () def show (): colorchooser. askcolor () Button (root, text = 'select color', command = show ). pack () mainloop ()

Result:

  

Finally, make a registration window

From tkinter import * from tkinter import messageboxroot = Tk () def show (): messagebox. askyesno ('mountain ',' Are you sure you want to register? ') Def create (): # top. attributes ('-alpha', 0.5) top = Toplevel (root) top. title ('mountain ') top. geometry ('400x250') Label (top, text = 'username :'). place (x = 20, y = 50) Label (top, text = 'password :'). place (x = 20, y = 120) Entry (top ). place (x = 110, y = 50) Entry (top, show = '*'). place (x = 110, y = 120) Button (top, text = 'Confirm registration', width = 10, command = show ). place (x = 170, y = 190) root. title ('mountain ') root. geometry ('Drawing x400') Label (root, text = 'username :'). place (x = 100, y = 170) Label (root, text = 'password :'). place (x = 100, y = 230) photo = PhotoImage(file='welcome.gif ') Label (root, image = photo ). place (x = 0, y = 0) Entry (root ). place (x = 190, y = 170) Entry (root, show = '*'). place (x = 190, y = 230) Button (root, text = 'registration', width = 10, command = create ). place (x = 100, y = 300) Button (root, text = 'Submit', width = 10 ). place (x = 300, y = 300) mainloop ()

Result:

    

References:

Python tutorial video

 

Related Article

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.