一、基础内容
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
39
40
41
|
import tkinter as tk from PIL import Image,ImageTk def my(): pwin.destroy() win.deiconify() win = tk.Tk() #win.geometry('320x240+100+50') #长320宽240 右100,下50 win[ 'width' ] = 320 #可以用win.keys()查看哪些可以这样写 win[ 'height' ] = 240 win.title( 'tkinter图形化专题学习' ) #win.iconbitmap('my.ico') img = ImageTk.PhotoImage(image = Image. open ( '1.gif' , 'r' )) win.iconphoto( False ,img) #win.withdraw()#临时退出 #win.deiconify()#恢复 #win.iconify()#最小化 #win.destroy()#关掉 #help(win.state) """ win.state('iconic') win.state('normal') win.state('zommed') win.state('zoomed') win.state('withdraw') """ #win.attributes('-alpha',0.5) #设置透明度 #win.attributes('-toolwindow',1)#没有最大化最小化按钮 #win.attributes('-toolwindow',0)#常规窗口 #win.attributes('-topmost',1) #置顶 win.withdraw() #隐藏原窗口 pwin = tk.Toplevel(win) #新建弹窗 pwin.title( '弹窗' ) pwin.protocol( 'WM_DELETE_WINDOW' ,my) win.mainloop() |
二、渐变窗口与计时器
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
|
#计时器 import tkinter as tk import time def my(): global x if x > 1 : return win.title( '计时器:{:.2f}' . format (x)) win.attributes( '-alpha' , x) x + = 0.01 win.after( 200 ,my) #100ms win = tk.Tk() win.title( "计时器" ) win[ 'bg' ] = 'lightblue' #用win.keys()查看 win.geometry( '300x300' ) win.resizable( 0 , 0 ) #不可变窗口大小 x = 0.01 win.attributes( '-alpha' , x) win.after( 200 ,my) win.mainloop() |
三、简单的布局标签文本框按钮
3.1 布局
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
import tkinter as tk win = tk.Tk() win.title( "布局" ) win.geometry( '320x240' ) #win.resizable(0,0) #定义 lbl = tk.Label(win,text = "请输入" ) txt = tk.Entry(win) btn = tk.Button(win,text = "确定" ) #布局 方法一pack打包式放置 方法二grid表格式放置 方法三place任意放置 """ lbl.pack(side='left' ,padx=(10,0)) txt.pack(side='left') #txt.pack_info()查看pack的参数 btn.pack(side='left', padx='10') """ """ lbl.grid(row=0,column=0,padx=(50,10),pady=10) #lbl.grid_info()查看grid的参数 txt.grid(row=0,column=1) btn.grid(row=1,column=0,columnspan=2,padx=(50,0),pady=10, sticky='we') """ lbl.place(relx = 0.1 ,rely = 0.2 ) txt.place(relx = 0.3 ,rely = 0.15 ) btn.place(relx = 0.5 ,rely = 0.25 ) #win.mainloop() |
3.2 布局
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
import tkinter as tk #建窗口 win = tk.Tk() win.title( '布局' ) #建组件 frm = tk.Frame(win) #框架 #定义 lbl = tk.Label(frm,text = '请输入' ) txt = tk.Entry(frm) btn = tk.Button(win,text = '确定' ) #布局 lbl.pack(side = 'left' , padx = 10 , pady = 10 ) txt.pack(side = 'left' , padx = 10 , pady = 10 ) frm.pack() btn.pack(fill = 'x' ) win.mainloop() |
四、摄氏度华氏度
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
39
40
41
42
43
44
45
46
47
|
import tkinter as tk def myfun(e): #def myfun(): try : a = int (entry1.get()) b = int (entry2.get()) lbx.delete( 0 , 'end' ) for i in range (a,b + 1 ): f = i * 9 / 5 + 32 lbx.insert( 'end' , ' {} {}' . format (i,f)) except : pass win = tk.Tk() win.title( '温度转换' ) win.resizable( 0 , 0 ) #大小不可更改 label1 = tk.Label(win, text = '请输入第1个整数' ) label2 = tk.Label(win, text = '请输入第2个整数' ) label3 = tk.Label(win, text = '摄氏温度 华氏温度' ) entry1 = tk.Entry(win) entry2 = tk.Entry(win) entry1.insert( 0 , '10' ) entry2.insert( 0 , '15' ) #btn=tk.Button(win, text='确定',command=myfun) btn = tk.Label(win, text = '确定' , #把label变成按钮 relief = 'groove' ) #浮雕 btn.bind( '<Button-1>' , myfun) #用bing绑定 必须给函数参数 lbx = tk.Listbox(win) #滚动条 scr = tk.Scrollbar(win) scr[ 'command' ] = lbx.yview lbx[ 'yscrollcommand' ] = scr. set label1.grid(row = 1 , column = 0 ) label2.grid(row = 2 , column = 0 ) label3.grid(row = 0 , column = 2 ) entry1.grid(row = 1 , column = 1 ) entry2.grid(row = 2 , column = 1 ) btn.grid(row = 3 , column = 1 , sticky = "ew" ) lbx.grid(row = 1 , column = 2 , rowspan = 3 , padx = 10 , pady = 10 ) scr.grid(row = 1 , column = 3 , rowspan = 3 , sticky = 'ns' ) #btn.key()找到可以设置的参数 或者btn.config() win.mainloop() |
五、随机弹出数字(随机点名、抽奖)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
import tkinter as tk import random def myfun(): lst = list ( range ( 100 )) name = random.choice(lst) lbl[ 'text' ] = name win.after( 500 ,myfun) win = tk.Tk() win.title( '抽奖程序' ) lbl = tk.Label(win,text = '***' , width = 6 ,font = '-size 48' ) btn = tk.Button(win,text = '开始' , font = '-size 36' , command = myfun) lbl.grid(row = 0 ,column = 0 ,padx = 10 ,pady = 10 ) btn.grid(row = 0 ,column = 1 ,padx = 10 ,pady = 10 ) #win.mainloop() |
六、几行几列五子棋画布
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
import tkinter as tk win = tk.Tk() win.title( '五子棋' ) win.geometry( '800x600+64+32' ) win.resizable( 0 , 0 ) #建画布 cav = tk.Canvas(win, width = 600 ,height = 600 , bg = 'yellow' ) rfm = tk.Frame(win,width = 200 ,height = 600 , bg = 'lightyellow' ) cav.pack(side = 'left' ) rfm.pack(side = 'right' ) #画线 for i in range ( 1 , 20 ): x1, y1, x2, y2 = 30 , i * 30 , 19 * 30 ,i * 30 cav.create_line(x1,y1,x2,y2) cav.create_line(y1,x1,y2,x2) |
到此这篇关于Python基础之tkinter图形化界面学习的文章就介绍到这了,更多相关Python tkinter图形化界面学习内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/qq_31949641/article/details/116229818