Event in tkinter tutorial (3)

Source: Internet
Author: User
Tags tkinter tutorial
'''Tkinter event (3 )'''
'''11. Two events are bound to one control simultaneously '''
#-*-Coding: cp936 -*-
# BIND: Events and processing functions
# Bind two events to the same component
# Bind two events to the root user
From tkinter import *
Root = TK ()
# Key event processing functions
Def printevent (event ):
Print '<key>', event. keycode
# Return event processing functions
Def printreturn (event ):
Print '<return>', event. keycode
Root. BIND ('<key>', printevent)
Root. BIND ('<return>', printreturn)

Root. mainloop ()
# Printevent processes keys except return.
# When the key is return, printreturn is used to handle the event, that is, the event with the "nearest.
'''12. bind an event to an instance. '''
# BIND: event handler function bound to the instance
#-*-Coding: cp936 -*-
From tkinter import *
Root = TK ()
# Key event processing functions
Def printevent (event ):
Print '<key>', event. keycode
# Return event processing functions
Def printreturn (event ):
Print '<return>', event. keycode
# Use Bt1 to add an event handler.
Bt1 = button (root, text = 'instance event ')
Bt1.bind ('<key>', printevent)
Bt1.focus _ set ()
Bt1.grid ()

Root. mainloop ()
# When the key is pressed, the program calls the printevent once.
'''13. Event-level audio transfer '''
#-*-Coding: cp936 -*-
# BIND: bind instance and toplevel
# Bind_class: bind a class Handler
# Bind_all: bind all events of the application
# "Transfer" between event levels"
From tkinter import *
Root = TK ()
# Key event processing functions
Def printevent (event ):
Print '<instance>', event. keycode
# Return event processing functions
Def printtoplevel (event ):
Print '<toplevel>', event. keycode
Def printclass (event ):
Print '<bind_class>', event. keycode
Def printappall (event ):
Print '<bind_all>', event. keycode

# Binding to printevent at the instance level
Bt1 = button (root, text = 'instance event ')
Bt1.bind ('<return>', printevent)

# Bind the toplevel of Bt1 to printtoplevel
Bt1.winfo _ toplevel (). BIND ('<return>', printtoplevel)

# Bind the event printclass at the class level
Root. bind_class ('click', '<return>', printclass)

# Bind printappall at application all level
Bt1.bind _ all ('<return>', printappall)

# Focus on Bt1 and press Enter. Four output prints are displayed.
Bt1.focus _ set ()
Bt1.grid ()

Root. mainloop ()
# Output result:
# <Instance> 13
# <Bind_class> 13
# <Toplevel> 13
# <Bind_all> 13
# Return: "pass" to the high level. The call order is instance/class/toplevel/All.
'''14. Consequences of using bind_class '''
#-*-Coding: cp936 -*-
# Bind_class: bind the event handler function of the entire class, which will affect all instances of this class.
From tkinter import *
Root = TK ()

Def printclass (event ):
Print '<bind_class>', event. keycode

# Changing the event binding of the button class
Root. bind_class ('click', '<return>', printclass)
# Create two buttons
Bt1 = button (root, text = 'a button ')
Bt2 = button (root, text = 'another click ')

Bt1.focus _ set ()
Bt1.grid ()
Bt2.grid ()

Root. mainloop ()
# Press enter and Bt1 print the result
# Switch the tab to bt2, and press enter to print the result, that is, all buttons respond to the return event.
'''15. Use protocal to bind '''
#-*-Coding: cp936 -*-
# Protocol: interacts with WM and binds event handlers.
From tkinter import *
Root = TK ()

Def printprotocol ():
Print 'wm _ delete_window'
Root. Destroy ()

# Use protocol to bind wm_delete_window to printprotocol
Root. Protocol ('wm _ delete_window ', printprotocol)
Root. mainloop ()
# Print 'wm _ delete_window 'when the program exits'

 

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.