Python基礎之tkinter圖形化界面學習

一、基礎內容

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圖形化界面學習內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: