Python Tkinter: Action

Source: Internet
Author: User

Python Tkinter: Action
Interface programming requires the following important parts:
<! -- [If! SupportLists] --> tables <! -- [Endif] --> form
<! -- [If! SupportLists] --> tables <! -- [Endif] --> Control
<! -- [If! SupportLists] --> tables <! -- [Endif] --> action
A form is a container, and various controls are placed in the container. Each control executes certain commands after being triggered, that is, its actions are completed.
How can I bind a control to a command? Generally, when creating a control, use the command parameter to specify the action to be executed. This action can be used in the following situations:
<! -- [If! SupportLists] --> tables <! -- [Endif] --> Common Function
<! -- [If! SupportLists] --> tables <! -- [Endif] --> other member functions of the same class
<! -- [If! SupportLists] --> tables <! -- [Endif] --> built-in functions
<! -- [If! SupportLists] --> tables <! -- [Endif] --> lamda Function
How to create a window? Generally, there are several methods, one of which is procedural, using Tk (). Another type is object-oriented, that is, the objects returned from Frame. Tk () are the root container, and the objects generated by Frame can be placed in the objects returned by Tk.
After each window is created, use pack to layout and make it visible to you. You can use expand, fill, and side for layout.
The bind function is called by the control to bind the event. The first parameter is the string representing the event, and the second parameter is the action to be executed. The source of the action is described earlier.
Here are some examples:
 
1 .#! /Usr/bin/python
2.
3.
4.
5. from Tkinter import *
6.
7.
8.
9. def quit ():
10.
11. print "I have to leave now ..."
12.
13. import sys
14.
15. sys. exit ()
16.
17.
18.
19. B = Button (None, text = "quit", bg = "red", command = quit)
20.
21. B. pack ()
22.
23. B. mainloop ()
24.
25.
Create a Button and click it to exit.
 
 
1 .#! /Usr/bin/python
2.
3.
4.
5. from Tkinter import *
6.
7.
8.
9. class ClassCall ():
10.
11. def _ init _ (self ):
12.
13. self. msg = "call from a class. \ n"
14.
15. def _ call _ (self ):
16.
17. print self. msg
18.
19. import sys
20.
21. sys. exit ()
22.
23.
24.
25. widget = Button (None, text = "test", command = ClassCall ())
26.
27. widget. pack ()
28.
29. widget. mainloop ()

Specify an instance of a class as an action. By default, the _ call _ method of the class is called.
 
 
1 .#! /Usr/bin/python
2.
3.
4.
5. from Tkinter import *
6.
7.
8.
9. class InnerClass ():
10.
11. def _ init _ (self ):
12.
13. self. B = Button (None, text = "test", command = self. call)
14.
15. self. B. pack ()
16.
17. def call (self ):
18.
19. print "I am leaving now ..."
20.
21. import sys
22.
23. sys. exit ()
24.
25.
26.
27. InnerClass ()
28.
29. mainloop ()
30.
31.
Call member functions within the same class
 
1.
2.
3 .#! /Usr/bin/python
4.
5.
6.
7. from Tkinter import *
8.
9.
10.
11. class GuiDesign ():
12.
13. def _ init _ (self, parent = None ):
14.
15. self. top = Frame (parent)
16.
17. self. top. pack ()
18.
19. self. data = 0
20.
21. self. layout ()
22.
23.
24.
25. def layout (self ):
26.
27. Button (self. top, text = "exit", command = self. top. quit). pack (side = LEFT)
28.
29. Button (self. top, text = "hi", command = self. hi). pack (side = RIGHT)
30.
31.
32.
33. def hi (self ):
34.
35. self. data + = 1
36.
37. print "hi: % d" % self. data
38.
39.
40.
41. frm = Frame ()
42.
43. frm. pack () # easy to make mistake here.
44.
45. Label (frm, text = "hello"). pack (side = TOP)
46.
47. GuiDesign (frm). top. mainloop ()
48.
49.
Use Frame Objects as parameters
 
 
1 .#! /Usr/bin/python
2.
3.
4.
5. from Tkinter import *
6.
7.
8.
9. def showPosEvent (event ):
10.
11. print 'widget = % s X = % s Y = % s' % (event. Widget, event. x, event. y)
12.
13. print 'got key perss: ', event. char
14.
15.
16.
17.
18.
19. tkroot = Tk ()
20.
21. labelfont = ('courier ', 20, 'bold ')
22.
23. widget = Label (tkroot, text = 'Hello bind World ')
24.
25. widget. config (bg = 'red', font = labelfont)
26.
27. widget. config (height = 5, width = 20)
28.
29. widget. pack (expand = YES, fill = BOTH)
30.
31.
32.
33. widget. bind ('<KeyPress>', showPosEvent)
34.
35. widget. focus ()
36.
37. tkroot. title ('click me ')
38.
39. tkroot. mainloop ()
Responds to button events and dynamically configures controls

 

 

From believing and loving

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.