Python GUI學習筆記(一):使用Tkinter

#20200430更新Tkinter GUI設計 @ K_程式人 :: 痞客邦 ::

建立視窗

from tkinter import *

window = Tk()

window.title("我的第一個GUI程式") #視窗標題

window.mainloop()

在視窗中顯示文字

from tkinter import *

window = Tk()

window.title(“Welcome to LikeGeeks app”)
window.geometry('350x200') #視窗尺寸
lbl = Label(window, text=”Hello”) #顯示文字
lbl.grid(column=0, row=0) #文字位置
lbl = Label(window, text="Hello", font=("Arial Bold", 50)) #字體與大小
btn = Button(window, text="Click Me") #新增按鈕
btn.grid(column=1, row=0) #按鈕位置
btn = Button(window, text="Click Me", bg="orange", fg="red")
#改變前景與背景色
window.mainloop()

處理按鈕事件

def clicked():
lbl.configure(text="Button was clicked !!")
------------------------------------------------------------from tkinter import *window = Tk()window.title(“Welcome to LikeGeeks app”)window.geometry(‘350x200’)lbl = Label(window, text=”Hello”)lbl.grid(column=0, row=0)def clicked(): lbl.configure(text=”Button was clicked !!”)btn = Button(window, text=”Click Me”, command=clicked)
#使用clicked函數
btn.grid(column=1, row=0)window.mainloop()

修改按鈕事件

def clicked():

res = "Welcome to " + txt.get() #取得輸入到txt的文字內容

lbl.configure(text= res)
-----------------------------------------------------------------
from tkinter import *
window = Tk()window.title("Welcome to LikeGeeks app")window.geometry('350x200')lbl = Label(window, text="Hello")lbl.grid(column=0, row=0)txt = Entry(window,width=10)txt.grid(column=1, row=0)def clicked():
res = "Welcome to " + txt.get()
lbl.configure(text= res)
btn = Button(window, text="Click Me", command=clicked)btn.grid(column=2, row=0)window.mainloop()

輸入文字

txt = Entry(window,width=10)      #文字輸入框,寬度10
-----------------------------------------------
from tkinter import *
window = Tk()window.title("Welcome to LikeGeeks app")window.geometry('350x200')lbl = Label(window, text="Hello")lbl.grid(column=0, row=0)txt = Entry(window,width=10)txt.grid(column=1, row=0)def clicked():lbl.configure(text="Button was clicked !!")btn = Button(window, text="Click Me", command=clicked)btn.grid(column=2, row=0)window.mainloop()

取消輸入框

txt = Entry(window,width=10, state='disabled')

下拉式方框

from tkinter.ttk import *
combo = Combobox(window)
------------------------------------------------

from tkinter import *
from tkinter.ttk import *window = Tk()window.title("Welcome to LikeGeeks app")window.geometry('350x200')combo = Combobox(window)combo['values']= (1, 2, 3, 4, 5, "Text")combo.current(1) #set the selected itemcombo.grid(column=0, row=0)window.mainloop()

複選框

chk = Checkbutton(window, text='Choose')
-------------------------------------------------
from tkinter import *from tkinter.ttk import *window = Tk()window.title("Welcome to LikeGeeks app")window.geometry('350x200')chk_state = BooleanVar()chk_state.set(True) #set check statechk = Checkbutton(window, text='Choose', var=chk_state)chk.grid(column=0, row=0)window.mainloop()

選項按鈕

rad1 = Radiobutton(window,text='First', value=1)---------------------------------------------------from tkinter import *from tkinter.ttk import *

window = Tk()

window.title("Welcome to LikeGeeks app")

window.geometry('350x200')

rad1 = Radiobutton(window,text='First', value=1)

rad2 = Radiobutton(window,text='Second', value=2)

rad3 = Radiobutton(window,text='Third', value=3)


rad1.grid(column=0, row=0)

rad2.grid(column=1, row=0)

rad3.grid(column=2, row=0)

window.mainloop()

取得選項按鈕的值

from tkinter import *

from tkinter.ttk import *

window = Tk()

window.title("Welcome to LikeGeeks app")

selected = IntVar()

rad1 = Radiobutton(window,text='First', value=1, variable=selected)

rad2 = Radiobutton(window,text='Second', value=2, variable=selected)

rad3 = Radiobutton(window,text='Third', value=3, variable=selected)

def clicked():

print(selected.get())


btn = Button(window, text="Click Me", command=clicked)

rad1.grid(column=0, row=0)

rad2.grid(column=1, row=0)

rad3.grid(column=2, row=0)

btn.grid(column=3, row=0)

window.mainloop()

滾動文字框

from tkinter import scrolledtexttxt = scrolledtext.ScrolledText(window,width=40,height=10)-----------------------------------------------------------------from tkinter import *

from tkinter import scrolledtext

window = Tk()

window.title("Welcome to LikeGeeks app")

window.geometry('350x200')

txt = scrolledtext.ScrolledText(window,width=40,height=10)
txt.insert(INSERT,'You text goes here') #插入文字到滾動框txt.grid(column=0,row=0)

window.mainloop()

創建對話框

from tkinter import messagebox

messagebox.showinfo('Message title','Message content')
------------------------------------------------
from tkinter import *

from tkinter import messagebox

window = Tk()

window.title("Welcome to LikeGeeks app")

window.geometry('350x200')

def clicked():

messagebox.showinfo('Message title', 'Message content')


btn = Button(window,text='Click here', command=clicked)

btn.grid(column=0,row=0)

window.mainloop()
-----------------------------------------------------------------
messagebox.showwarning('Message title', 'Message content') #顯示警告messagebox.showerror('Message title', 'Message content') #顯示錯誤

問題對話框

from tkinter import messagebox

res = messagebox.askquestion('Message title','Message content')

res = messagebox.askyesno('Message title','Message content')

res = messagebox.askyesnocancel('Message title','Message content')

res = messagebox.askokcancel('Message title','Message content')

res = messagebox.askretrycancel('Message title','Message content')

數字選擇

spin = Spinbox(window, from_=0, to=100)            #範圍0~100
spin = Spinbox(window, from_=0, to=100, width=5) #範圍0~100,寬度5
-------------------------------------------------------
from tkinter import *

window = Tk()

window.title("Welcome to LikeGeeks app")

window.geometry('350x200')

spin = Spinbox(window, from_=0, to=100, width=5)

spin.grid(column=0,row=0)

window.mainloop()
-------------------------------------------------------
spin = Spinbox(window, values=(3, 8, 11), width=5) #只顯示3,8,11
#設定數字預設值
var =IntVar()
var.set(36)
spin = Spinbox(window, from_=0, to=100, width=5, textvariable=var)

進度條

from tkinter.ttk import Progressbar

bar = Progressbar(window, length=200)
-------------------------------------------------------------------
from tkinter import *

from tkinter.ttk import Progressbar

from tkinter import ttk

window = Tk()

window.title("Welcome to LikeGeeks app")

window.geometry('350x200')

style = ttk.Style()

style.theme_use('default')

style.configure("black.Horizontal.TProgressbar", background='black')

bar = Progressbar(window, length=200, style='black.Horizontal.TProgressbar')

bar['value'] = 70 #進度條至70%

bar.grid(column=0, row=0)

window.mainloop()

選取檔案路徑

from tkinter import filedialog

file = filedialog.askopenfilename() #單一檔案
files =filedialog.askopenfilenames()#多個檔案,函式名稱只差一個s
dir = filedialog.askdirectory() #選擇指定資料夾路徑
---------------------------------------------------------
from os import path
file = filedialog.askopenfilename(initialdir= path.dirname(__file__)) #指定起始選擇資料夾位置

file = filedialog.askopenfilename(filetypes = (("Text files","*.txt"),("all files","*.*"))) #指定檔案類型

增加MENU導航欄

from tkinter import Menumenu = Menu(window)menu.add_command(label='File')window.config(menu=menu)-----------------------------------
from tkinter import *

from tkinter import Menu

window = Tk()

window.title("Welcome to LikeGeeks app")

menu = Menu(window)

new_item = Menu(menu)

new_item.add_command(label='New')

menu.add_cascade(label='File', menu=new_item) #在menu裡建立menu

window.config(menu=menu)

window.mainloop()
----------------------------------------------------------------
建立更多MENU
from tkinter import *

from tkinter import Menu

window = Tk()

window.title("Welcome to LikeGeeks app")

menu = Menu(window)

new_item = Menu(menu)

new_item.add_command(label='New')

new_item.add_separator()

new_item.add_command(label='Edit')

menu.add_cascade(label='File', menu=new_item)

window.config(menu=menu)

window.mainloop()

建立Tab頁面

#單頁
from tkinter import *

from tkinter import ttk

window = Tk()

window.title("Welcome to LikeGeeks app")

tab_control = ttk.Notebook(window)

tab1 = ttk.Frame(tab_control)

tab_control.add(tab1, text='First')

tab_control.pack(expand=1, fill='both')

window.mainloop()
#多頁
from tkinter import *

from tkinter import ttk

window = Tk()

window.title("Welcome to LikeGeeks app")

tab_control = ttk.Notebook(window)

tab1 = ttk.Frame(tab_control)

tab2 = ttk.Frame(tab_control)

tab_control.add(tab1, text='First')

tab_control.add(tab2, text='Second')


lbl1 = Label(tab1, text= 'label1')

lbl1.grid(column=0, row=0)

lbl2 = Label(tab2, text= 'label2')

lbl2.grid(column=0, row=0)

tab_control.pack(expand=1, fill='both')

window.mainloop()

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store