用python的tkinter模块开发桌面应用

demo
原文

1. 安装python

click here to download
双击安装(我用的是3.5.1版本)

2. 运行脚本

创建 trinker.py
写以下代码
双击运行代码

3. 入门

1
2
3
4
5
6
7
# import the tkinter core package
import tkinter
# create a root window with tkinter
root = tkinter.Tk()
## start the event loop
## (actually i don't know what does it look like in python at this time)
root.mainloop()

4. 窗口参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# print (root.keys) # printing the all options
# root['key'] = value
root['bg'] = 'black' # same to background
# relief
# screen
# use
# colormap
# container
root['cursor'] = 'abc.png' # cursor image
root['bd'] = '2' # same to borderwidth
root['height'] = '400' # height
# highlightbackground # 部件具有焦点时的亮点区域的背景颜色.
# highlightcolor # 前景色的亮点区域,部件具有焦点时.
# highlightthickness # 焦点时边框宽度
root['padx'] = '10' # inner padding x
root['pady'] = '10' # inner padding y
# takefocus # 获取焦点的键盘定义
# visual
root['width'] = '400' # width
root['menu'] = myMenu # add myMenu

5. 窗口函数

1
2
3
4
5
6
7
8
9
10
# set the window's title
root.title("this is a window created with tkinter in python")
# set the size of the window(WxH+X+Y)
root.geometry('600x500+10+20');
# set the resizability
root.resizable(width=True,height=False)
# icon of the window
root.iconbitmap('gif/003.ico')
# enter the event loop
root.mainloop()

7. 几何布局之pack

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
elem.pack(**options)
# same ad pack
pack_configure()
# remome , but not destroy , can display again with pack()
pack_forget()
# Returns a dictionary containing the current packer options.
pack_info()
# (Manager method) Controls geometry propagation. If enabled, the manager widget will be resized if not large enough to hold all the child widgets.
pack_propagate(flag)
# (Manager method) Returns a list of all child (“slave”) widgets managed by the packer for this widget.Returns: A list of child widgets.
pack_slaves()
# Where the widget is placed inside the packing box. Default is CENTER.
anchor = center,x,e,nw,ne,se,sw,s,n
# Specifies whether the widgets should be expanded to fill any extra space in the geometry master. If false (default), the widget is not expanded.
expand = True,false
# Specifies whether the widget should occupy all the space provided to it by the master.
fill = none,both,x,y
# Pack this widget inside the given widget.
in =
# inner pading
ipadx = 0
ipady = 0
# outer padding
padx = 0
pady = 0
# Specifies which side to pack the widget against.
side = left,right,top,bottom

7. 几何布局之grid

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
elem.grid(**options)
# Returns a list of the “slave” widgets managed by this widget.
grid_slaves(row = 'none', column = 'none')
# Returns the current grid size for the geometry manager attached to this widget.
grid_size()
# Set options for a row,column of cells.
# padsize=,pad=,weight=
elem.grid_rowconfigure(index, **options)
grid_columnconfigure(index, **options)
# Enables or disables geometry propagation.
girid_propagate(flag) True,False
# Returns the grid cell under (or closest to) a given pixel.
grid_location(x, y)
# Return a dictionary containing the current cell options for the cell used by this widget.
grid_info()
# Remove this widget from the grid manager. The widget is not destroyed, and can be displayed again by grid or any other manager.
grid_forget()
# Same as grid.
grid_configure(**options) [#]
# The grid_bbox method.
grid_bbox(column=None, row=None, col2=None, row2=None)
# column row number
column = 1
row = 2
# If given, indicates that the widget cell should span multiple columns or rows. The default is 1.
columnspan = 2
rowspan = 3
# Place widget inside to the given widget.
in =
in_ = # same as in
# the inner paddings
ipadx =
ipady =
# the outer paddings
padx =
pady =
# Defines how to expand the widget if the resulting cell is larger than the widget itself.
sticky = w,e,n,s,se,we,ne,nw

7. 几何布局之place

1
2
3
4
5
6
7
8
9
10
11
elem.place(**options)
# Same as place.
place_configure(**options)
# The place_forget method.
place_forget()
# The place_info method.
place_info()
# The place_slaves method.
place_slaves()
Same as place_slaves.
slaves()

8. 常用颜色定义

  • bg: 背景颜色

  • fg:前景颜色

  • activebackground: 当widget是活跃widget的背景颜色.

  • activeforeground:小部件时,部件是活跃的前景色.

  • background: widget的背景颜色。这也可以代表ASBG.

  • disabledforeground: 前景色的部件,当部件被禁用.

  • foreground: widget的前景色。这也可以表示为FG.

  • highlightbackground: 部件具有焦点时的亮点区域的背景颜色.

  • highlightcolor: 前景色的亮点区域,部件具有焦点时.

  • selectbackground: widget的所选项目的背景颜色.

  • selectforeground: 前景色为widget的选定项目.

×

纯属好玩

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

文章目录
  1. 1. 1. 安装python
  2. 2. 2. 运行脚本
  3. 3. 3. 入门
  4. 4. 4. 窗口参数
  5. 5. 5. 窗口函数
  6. 6. 7. 几何布局之pack
  7. 7. 7. 几何布局之grid
  8. 8. 7. 几何布局之place
  9. 9. 8. 常用颜色定义
,