Related content:
Starting Time: 2018-03-04 19:26
Command
- command is a parameter in the control, and if the command= function is made, the function will be triggered when the control is clicked
- Common controls that can define a command are: Button, Menu ...
- When calling a function, the default is that no arguments are passed in, and if you want to force incoming parameters, you might consider using lambda
fromTkinterImport*Root=Tk ()defprt ():Print("Hello")defFunc1 (*args,**Kwargs):Print(*args,**Kwargs) hello_btn=button (root,text="Hello", COMMAND=PRT)#Demohello_btn.pack () args_btn=button (root,text="Learn if the button event has parameters by default", COMMAND=FUNC1)#Knowing if there are parameters, the result is noargs_btn.pack () btn1=button (root,text="Transfer Parameters", command=Lambda: Func1 ("Running"))#Force Transfer ParametersBtn1.pack () root.mainloop ( )
Bind
- Bind usage: Control. Bind (event, Handler), where event is tkinter already defined events, handler is the processor, can be a handler function, if the related event occurs, the handler function will be triggered, event object is passed to the handler function
- All basic controls can bind
- Common event is:
- Mouse Click event: left mouse button click for <button-1>, mouse click for <button-2>, right mouse click for <button-3>, scroll up pulley for <BUTTON-4> scroll down pulley for <button-5>.
- Mouse double-click event.: Click the left mouse button for <double-button-1>, click the middle mouse button for <double-button-2>, right mouse click for <double-button-3>
- Mouse Release event: left mouse button click for <buttonrelease-1>, mouse click for <buttonrelease-2>, right mouse button click for <buttonrelease-3> The position of the mouse relative to the current control is passed to the callback function in the X and Y fields stored in the event object.
- Mouse over control event:<enter>
- Get Focus Event:<focusin>
- Mouse out of control events: <Leave>
- Lost Focus Event:<focusout>
- Mouse Press Move event: left mouse button click for <b1-motion>, mouse click for <b2-motion>, right mouse button click for <b3-motion> The position of the mouse relative to the current control is passed to the callback function in the X and Y fields stored in the event object.
- Keyboard down event: Keysym in <key>,event, Keycode,char can get the key pressed "other want to get the value also can first see what is in the event"
- Keying event:<return> Enter, <backspace>,<escape>,<left>,<up>,<right>,<down> .
- Control size Change Event: <configure>, the new control size is stored in the event object in the width and Height properties passed. On some platforms, this event may also represent a change in the control's location.
- Properties in the event:
- Widget: The control that generated the event
- x, Y: The position of the current mouse
- X_root, Y_root: The position of the current mouse relative to the upper-left corner of the screen, in pixels.
- Char: Character code (keyboard event only) as a string.
- Keysym: Key symbol (keyboard event only).
- KeyCode: Key code (keyboard events only).
- num: Button number (mouse button event only).
- width, Height: The new size (in pixels) of the widget (configuration events only).
- Type: Event types.
fromTkinterImport*Root=Tk () root.geometry ("200x200") Text=Text (Root) text.pack ()deffunc (Event):Print(Event)defFunc_release (event):Print("Release")#Click#text.bind ("<Button-1>", func)#root.bind ("<Button-1>", func)#Double click#text.bind ("<Double-Button-1>", func)#Mouse Release#text.bind ("<ButtonRelease-1>", Func_release)#Mouse Move in#text.bind ("<Enter>", func)#Mouse down Move event#text.bind ("<B1-Motion>", func)#Keyboard Press Event#text.bind ("<Key>", func)#Keyed binding Event#def func3 (event):#print ("You pressed the carriage return!")#text.bind ("<Return>", func3)#implementation of a drag functiondefFunc4 (event):#print (event)x=Str (event.x_root) y=Str (event.y_root) root.geometry ("200x200+"+x+"+"+y) Text.bind ("<B1-Motion>", Func4) root.mainloop ()
Add: If you want to pass a parameter, you can use lambda:
Text.bind ("<Button-1>",Lambda event:func (event,"Hello "))
Protocol
- Protocol use: Control. Protocol (Protocol,handler), where the control is a Window object (tk,toplevel)
- Common protocol are:
- Wm_delete_window: The most commonly used protocol is called Wm_delete_window, which defines what happens when a user explicitly closes a window using the window manager. If you use your own handler to handle events, the window will not automatically close
- Wm_take_focus, wm_save_yourself:[These two don't know what's coming. ]
- More Reference ICCCM Documentation
- Note: Be mindful of the wording of the Protocol, do not add extra space when filling as a string
fromTkinterImport*ImportTkinter.messageboxroot=Tk () root.geometry ("200x200")deffunc1 ():ifTkinter.messagebox.askyesno ("Close Window","confirm Close window?"): Root.destroy () Root.protocol ("Wm_delete_window", Func1) root.mainloop ()
To learn more, refer to Tkinter's official documentation: http://effbot.org/tkinterbook/
Python:gui's Tkinter Learning Note 3 event bindings