本篇文章为大家展示了怎么在Python中使用tkinter图形化界面,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
python可以做什么
Python是一种编程语言,内置了许多有效的工具,Python几乎无所不能,该语言通俗易懂、容易入门、功能强大,在许多领域中都有广泛的应用,例如最热门的大数据分析,人工智能,Web开发等。
一、基础内容
import tkinter as tkfrom PIL import Image,ImageTkdef my(): pwin.destroy() win.deiconify()win = tk.Tk()#win.geometry('320x240+100+50') #长320宽240 右100,下50win['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()
二、渐变窗口与计时器
#计时器import tkinter as tkimport timedef 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.01win.attributes('-alpha', x)win.after(200,my)win.mainloop()
三、简单的布局标签文本框按钮
3.1 布局
import tkinter as tkwin=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 布局
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()
四、摄氏度华氏度
import tkinter as tkdef 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: passwin=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.yviewlbx['yscrollcommand']=scr.setlabel1.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()
五、随机弹出数字(随机点名、抽奖)
import tkinter as tkimport randomdef 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()
六、几行几列五子棋画布
import tkinter as tkwin=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图形化界面,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注编程网行业资讯频道。