欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python基础之tkinter图形化界面学习

程序员文章站 2022-04-05 11:41:23
一、基础内容import tkinter as tkfrom pil import image,imagetkdef my(): pwin.destroy() win.de...

一、基础内容

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()

二、渐变窗口与计时器

#计时器
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 布局

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 布局

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 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()

五、随机弹出数字(随机点名、抽奖)

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()

六、几行几列五子棋画布

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图形化界面学习内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!