Tk圖形介面程式庫

Steven Wang
10 min readJan 9, 2019

--

GUI (Graphical User Interface)

MVC模式 (Model View Controller)
Model 資料處理的核心類別 範例為Encrypt類別
View GUI圖形介面軟體類別 範例為EncryptView類別
Controller Model與View之間溝通的類別 範例為EncryptController類別

MVC分三個部分的原因是因為每一個部分都互相獨立互不干擾,例如改V換置成其他圖形介面程式庫Pyside或Kivy,M不用改變,C再根據V來設計。將介面與程式控制完全分開各自可以獨立修改

製作Tk應用程式的基本步驟
(1)建立Tk應用物件
(2)建立視窗物件
(3)加入視窗元件
(4)設定版面佈局(視窗元件在視窗中排列的方式)
(5)設定事件方法
(6)Tk應用物件呼叫mainloop()方法

Tk三種版面管理員
(1)包裹版面管理員 由Tk應用物件自動安排視窗元件的位置

from tkinter import *
root=Tk() #root為Tk應用程式物件
root.title(“title name”)#用title()設定視窗標題
text=Label(root,text=”Hello World”,width=”30”,height=”5”,bg=”black”,fg=”white”)
#text設定為label視窗元件,root表示這是root的視窗元件 ,
關鍵字text width height bg fg設定參數
text.pack()#pack()方法呼叫包裹面管理員
root.mainloop()

(2)定位版面管理員 自行設定視窗元件在視窗上的座標

視窗左上角為原點(0,0) x往右為正 y往下為正

from tkinter import *
root=Tk()
root.title(“title name”)
root.geometry(“276*86”)#呼叫geometry()方法去設定視窗尺寸
text=Label(root,text=”Hello World”,width=”30”,height=”5”,bg=”black”,fg=”white”)
text.place(x=0,y=0)#text呼叫place()方法,這是text用來設定在視窗中的起始座標
root.mainloop()

(3)格子版面管理員 依格子的方式設定視窗元件的位置(最常用)

from tkinter import *
root=Tk() #root為Tk應用程式物件
root.title(“title name”)
text=Label(root,text=”Hello World”,width=”30”,height=”5”,bg=”black”,fg=”white”)
text.grid(row=0,column=0)#grid()方法用row設定由上而下的第幾行由左而右的第幾列
root.mainloop()

用類別設計Tk應用程式的步驟

(1)定義繼承自Frame的類別,例如HelloView
(2)在__init__()方法中呼叫Frame的__init__(),進行相關設定,最後呼叫createWidgets()方法
(3)createWidgets()方法為自訂建立視窗元件的方法
(4)執行部分先建立Tk應用程式物件root,然後建立HelloView的物件,並以root當作master的參數,最後root呼叫mainloop()方法

Hello World類別版

from tkinter import *
class HelloView(Frame):
def __init__(self,master=None):#設定初值
[tab]Frame.__init__(self,master)
[tab]self.winfo_toplevel().title(“Hello World!”) #設定視窗標題用winfo_toplevel()方法
[tab]self.grid() #grid表示用格子版面管理員
[tab]self.createWidgets()

def createWidgets(self): #建立視窗文件 #屬性可以用字典的方式來設定
[tab]self.text=Label(self) #Label視窗元件
[tab]self.text[“text”]=”Hello World!”
[tab]self.text[“width”]=”30”
[tab]self.text[“height”]=”5”
[tab]self.text[“bg”]=”black”
[tab]self.text[“fg”]=”white”
[tab]self.text.grid(row=0,column=0)

if __name__==”__main__”:#GUI執行部分
root=Tk()
app=HelloView(master=root)#建立HelloView的物件,參數master要用Tk應用程式物件root代入
root.mainloop()

__init__有两个参数。第一个是self,即Application对象本身。第二个是master,在Tkinter中,一个控件可能属于另一个控件,这时另一个控件就是这个控件的master。默认一个窗口没有master,因此master有None的默认值

tk.Frame.__init__(self, master)调用Application的父类Frame的__init__函数初始化Application类的Frame类部分

因为这里不是直接用对象调用函数,又不是一般意义的创建对象(例如后面的quitButton),而是初始化自身的一部分,因此需要传入self。我们还传入了Application.__init__的master参数,作为Frame.__init__的master

self.createWidgets(),调用后面定义的createWidgets方法

app = Application(),创建一个名为app的Application对象

import tkinter as tk
class Application(tk.Frame):
def __int__(self,master):
[tab]tk.Frame.__int__(self,master)
[tab]self.grid()
[tab]self.createWidgets()

def createWedgets(self):
[tab]self.button = tk.Button(self)
[tab]self.button[“text”]=”demo”
[tab]self.button.grid(row=0,column=0,sticky=tk.N+tk.W)

[tab]self.checkbutton = tk.Checkbutton(self)
[tab]self.checkbutton[“text”]=”demo”
[tab]self.checkbutton.grid(row=1,column=0,sticky=tk.N+tk.W)

[tab]self.entry=tk.Entry(self) #Entry為單行輸入的inputbox
[tab]self.entry.grid(row=2,column=0,sticky=tk.N+tk.W) #sticky=tk.N+tk.W 隔線對齊的方式N靠上W靠左

[tab]self.label=tk.Label(self)
[tab]self.label[“text”]=”demo”
[tab]self.label.grid(row=3,column=0,sticky=tk.N+tk.W)

[tab]self.listbox=tk.Listbox(self) #列表單選欄位
[tab]self.listbox[“hight”]=5
[tab]self.listbox.insert(1,”python”)
[tab]self.listbox.insert(2,”Java”)
[tab]self.listbox.insert(3,”Swift”)
[tab]self.listbox.insert(4,”JavaScript”)
[tab]self.listbox.insert(5,”C”)
[tab]self.listbox.grid(row=4,column=0,sticky=tk.N+tk.W)

[tab]self.optionList=(“python”,”Java”,”Swift”) #下拉式單選欄位
[tab]self.v = tk.StringVar()
[tab]self.v.set(“demo”)
[tab]self.optionmenu=tk.OptionMenu(self,self.v,*self.optionList)
[tab]self.optionmenu.grid(row=5,column=0,sticky=tk.N+tk.W)

[tab]self.radiobutton=tk.Radiobutton(self)
[tab]self.radiobutton[“text”]=”demo”
[tab]self.radiobutton.grid(row=6,column=0,sticky=tk.N+tk.W)

[tab]self.scale=tk.Scale(self)
[tab]self.scale.grid(row=7,column=0,sticky=tk.N+tk.W)

[tab]self.spinbox=tk.Spinbox(self)
[tab]self.spinbox.grid(row=8,column=0,sticky=tk.N+tk.W)

[tab]self.text = tk.Text(self)
[tab]self.text[“height”]=10
[tab]self.text[“width”]=50
[tab]self.text..grid(row=9,column=0,sticky=tk.N+tk.W)

if __name__=”__main__”:
root = tl.Tk()
app=Application(root)
root.mainloop()

--

--

Steven Wang
Steven Wang

No responses yet