1 import Tkinter
2 top = Tkinter.Tk()
3 top.mainloop()

 

Tkinter的部件:

Tkinter的提供各种控件,如按钮,标签和文本框,一个GUI应用程序中使用。这些控件通常被称为部件.

目前有15种Tkinter的部件。我们提出这些部件以及一个简短的介绍,在下面的表:

OperatorDescription
Button The Button widget is used to display buttons in your application.
Canvas The Canvas widget is used to draw shapes, such as lines, ovals, polygons, and rectangles, in your application.
Checkbutton The Checkbutton widget is used to display a number of options as checkboxes. The user can select multiple options at a time.
Entry The Entry widget is used to display a single-line text field for accepting values from a user.
Frame The Frame widget is used as a container widget to organize other widgets.
Label The Label widget is used to provide a single-line caption for other widgets. It can also contain images.
Listbox The Listbox widget is used to provide a list of options to a user.
Menubutton The Menubutton widget is used to display menus in your application.
Menu The Menu widget is used to provide various commands to a user. These commands are contained inside Menubutton.
Message The Message widget is used to display multiline text fields for accepting values from a user.
Radiobutton The Radiobutton widget is used to display a number of options as radio buttons. The user can select only one option at a time.
Scale The Scale widget is used to provide a slider widget.
Scrollbar The Scrollbar widget is used to add scrolling capability to various widgets, such as list boxes.
Text The Text widget is used to display text in multiple lines.
Toplevel The Toplevel widget is used to provide a separate window container.
Spinbox The Spinbox widget is a variant of the standard Tkinter Entry widget, which can be used to select from a fixed number of values.
PanedWindow A PanedWindow is a container widget that may contain any number of panes, arranged horizontally or vertically.
LabelFrame A labelframe is a simple container widget. Its primary purpose is to act as a spacer or container for complex window layouts.
tkMessageBox This module is used to display message boxes in your applications.

 Tkinter的部件的管理方法

  • pack()方法 - 这个的几何管理器组织之前,将其放置在他们的父widget块部件.

  • grid()方法 - 这的几何管理器组织表状结构中的小部件的父部件.

  • place()方法 - 这个的几何管理器组织放置在一个特定的位置,在他们的父widget部件.

 

pack()

 

  pack几何管理采用块的方式组织配件,在快速生成界面设计中广泛采用,若干组件简单的布局,采用pack的代码量最少。pack几何管理程序根据组件创建生成的顺序将组件添加到父组件中去。通过设置相同的锚点(anchor)可以将一组配件紧挨一个地方放置,如果不指定任何选项,默认在父窗体中自顶向下添加组件。

 

  使用pack()布局的通用公式为:WidgetObject.pack(option, …)

 

  pack方法提供了下列option选项,选项可以直接赋值或以字典变量加以修改:

 

名称

描述

取值范围

expand

当值为“yes”时,side选项无效。组件显示在父配件中心位置;若fill选项为”both”,则填充父组件的剩余空间。

“yes”, 自然数, “no”, 0

 (默认值为“no”或0)

fill

填充x(y)方向上的空间,当属性side=”top”或”bottom”时,填充x方向;当属性side=”left”或”right”时,填充”y”方向;当expand选项为”yes”时,填充父组件的剩余空间。

“x”, “y”, “both”

(默认值为待选)

ipadx, ipady

组件内部在x(y)方向上填充的空间大小,默认单位为像素,可选单位为c(厘米)、m(毫米)、

i(英寸)、p(打印机的点,即1/27英寸),用法为在值后加以上一个后缀既可。

非负浮点数

(默认值为0.0)

padx, pady

组件外部在x(y)方向上填充的空间大小,默认单位为像素,可选单位为c(厘米)、m(毫米)、

i(英寸)、p(打印机的点,即1/27英寸),用法为在值后加以上一个后缀既可。

非负浮点数

(默认值为0.0)

side

定义停靠在父组件的哪一边上。

“top”, “bottom”, “left”, “right”

(默认为”top”)

before

将本组件于所选组建对象之前pack,类似于先创建本组件再创建选定组件。

已经pack后的组件对象

after

将本组件于所选组建对象之后pack,类似于先创建选定组件再本组件。

已经pack后的组件对象

in_

将本组件作为所选组建对象的子组件,类似于指定本组件的master为选定组件。

已经pack后的组件对象

anchor

对齐方式,左对齐”w”,右对齐”e”,顶对齐”n”,

底对齐”s”

“n”, “s”, “w”, “e”, “nw”, “sw”, “se”, “ne”, “center”

(默认为” center”)

 

 

grid方法提供了下列option选项,选项可以直接赋值或以字典变量加以修改:

 

名称

描述

取值范围

column

组件所置单元格的列号。

自然数(起始默认值为0,而后累加)

columnspan

从组件所置单元格算起在列方向上的跨度。

自然数(起始默认值为0)

ipadx, ipady

组件内部在x(y)方向上填充的空间大小,默认单位为像素,可选单位为c(厘米)、m(毫米)、

i(英寸)、p(打印机的点,即1/27英寸),用法为在值后加以上一个后缀既可。

非负浮点数

(默认值为0.0)

padx, pady

组件外部在x(y)方向上填充的空间大小,默认单位为像素,可选单位为c(厘米)、m(毫米)、

i(英寸)、p(打印机的点,即1/27英寸),用法为在值后加以上一个后缀既可。

非负浮点数

(默认值为0.0)

row

组件所置单元格的行号。

自然数(起始默认值为0,而后累加)

rowspan

从组件所置单元格算起在行方向上的跨度。

自然数(起始默认值为0)

in_

将本组件作为所选组建对象的子组件,类似于指定本组件的master为选定组件。

已经pack后的组件对象

sticky

组件紧靠所在单元格的某一边角。

“n”, “s”, “w”, “e”, “nw”, “sw”, “se”, “ne”, “center”

(默认为” center”)

 

tkMessageBox dialogs module, provides an assortment of common pop-ups for simple tasks.

tkFileDialog module, allows the user to browse for files.

tkColorChooser module, allows the user to select a color.

Tkinter 8.5 reference: http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/index.html

计算器

 1 #coding:utf-8
 2 from Tkinter import *
 3 class App:
 4     def __init__(self, master):
 5         master.geometry('200x300')
 6         master.title('Calc')
 7         menubar=Menu(master)
 8         menubar.add_command(label='Quit',command= master.destroy)
 9         helpmenu=Menu(menubar,tearoff=0)
10         helpmenu.add_command(label='About', command=lambda:self.dialog(master) )
11         menubar.add_cascade(label='Help',menu=helpmenu)
12         master["menu"]=menubar
13         frame1 = Frame(master)
14         frame1.pack(expand=1)
15         frame = Frame(master)
16         frame.pack(expand=1)
17         bstr='1234567890+-=*/'
18         for r in range(5):
19             for c in range(3):
20                 bch=bstr[r*3+c]
21                 Button(frame, text=bch,command=lambda ch=bch: self.callback(ch) ).grid(row=r,column=c,ipadx=10)
22 ##        frame.columnconfigure(0,minsize=1,weight=1)
23 ##        frame.rowconfigure(0,weight=1)
24         w = Label(frame1,text="输入结果")
25         w.pack()
26         self.v= StringVar()
27         e = Entry(frame1, textvariable=self.v)
28         e.pack(padx=5)
29         w1 = Label(frame1,text="计算结果")
30         w1.pack()
31         self.v1 = StringVar()
32         e1 = Entry(frame1, textvariable=self.v1)
33         e1.pack()
34     def callback(self,s):
35         if s in '1234567890':
36             self.v.set(self.v.get()+str(s))
37         elif s in '+-*/':
38             self.v.set((self.v.get() if self.v.get() else self.v1.get())+s)
39         elif s=='=':
40             input_str = self.v.get()
41             self.v1.set(eval(input_str))
42             self.v.set('')
43     def dialog(self,master):
44         top=Toplevel(master)
45         Label(top,text='Hello Calc!').pack()
46         Button(top,text='OK',command=top.destroy).pack()
47         master.wait_window(top)
48 root = Tk()
49 app = App(root)
50 root.mainloop()
51 
52 
53 ##for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5)
54 ##feet_entry.focus()
55 ##root.bind('<Return>', calculate)

 

posted on 2015-08-09 00:04  perel  阅读(969)  评论(0编辑  收藏  举报