用Tkinter打造GUI開發工具(23)軟件窗口皮膚

@用Tkinter打造GUI開發工具(23)軟件窗口皮膚

用Tkinter打造GUI開發工具(23)軟件窗口皮膚

Tkinter 是 Python 的標準 GUI 庫,它是一個跨平臺的腳本圖形界面接口(GUI)程序,且可以快速的創建 GUI 應用程序。Tkinter庫是一個跨平臺的窗口應用程序,使用它設計的程序可以在Windows、Mac、Linux系統上執行。由於 Tkinter 是內置到 Python 的安裝包中,只要安裝好 Python 之後就能導入Tkinter 庫,而且Python自帶編輯器IDLE 也是用 Tkinter 編寫而成。Tkinter能夠開發各種應用程序窗口界面。一些常見的庫,例如Turtle庫,EasyGui庫都是建立在Tkinter庫的基礎上。用戶可以輕鬆運用複製、粘貼、修改等方式,快速搭建用戶自己的GUI程序界面。
很多朋友認爲Tkinter做的軟件很醜,這都是誤解.因爲這些朋友不會Tkinter. 我們這節介紹Tkinter的美化設計.
首先看看我用Tkinter開發的軟件界面,這個界面有背景音樂。
在這裏插入圖片描述
相關代碼如下,中間彈出窗口的文字都是程序生成的.

g.root = tk.Tk()
pygame.mixer.init()
musicfile='mus/1.mid'  #背景音樂名
pygame.mixer.music.load(musicfile)  #加載背景音樂文件
pygame.mixer.music.set_volume(0.4)  #設置背景音量
pygame.mixer.music.play(loops=2)   #播放一次
g.root.title(g.title) 
window1 = tk.IntVar(g.root, value=0)
pil_image =Image.open('img//bj.jpg')
#獲取圖像的原始大小  
w, h = pil_image.size  
w2=w+1
h2=h+10        
pil_image = resize(w, h, w2 , h2, pil_image)  
r2,g2,b2=RGB(g.cns['gold'])
drawFont2(pil_image,10,10,"小白量化框架聚寬專版",size=40,r=r2,g=g2,b=b2)
r2,g2,b2==RGB(g.cns['gold'])
drawFont2(pil_image,80,50,"--大衆化基於人工智能的量化投資系統",size=26,r=r2,g=g2,b=b2)
drawFont3(pil_image,10,70,"超越!",size=80,r=255,g=0,b=0)
drawFont3(pil_image,110,140,"是我們的每一步!",size=54,r=255,g=255,b=0)
tk_image = ImageTk.PhotoImage(pil_image)  
username = tk.StringVar()  
password = tk.StringVar()  

def loginCheck():  
    name = username.get()  
    secret =password.get()  
    try:
        x=auth(name,secret)  #用戶登錄,name是用戶名,secret是用戶密碼
    except:
        x="error"
    if x!='error' or x=='' :
        g.login=True
        pygame.mixer.music.stop()  #停止背景音樂
        pygame.mixer.quit() #退出音樂mixer模塊
        g.status.set(5,'登錄')  #格式輸出信息
        g.status.text(1,'') #在狀態欄2輸出信息
        w1.destroy()
    else:  
        g.status.text(2,'賬號或密碼錯誤!') #在狀態欄2輸出信18578息            
        showinfo(title='錯誤', message='賬號或密碼錯誤!')
        g.login=False

def rtnkey(self,event=None):
    loginCheck()

w1 = myWindow(g.root,g.title,w2,h2)  #創建彈出窗口
showIco(w1.top,g.ico)  #設置窗口圖標
w1.overturn() #隱藏窗口邊框
label=tk.Label(w1.top,image=tk_image)
label.place(x=0, y=0, relwidth=1, relheight=1,bordermode=tk.OUTSIDE)
    Label(w1.top, text = '聚寬賬戶: ',font = 'Helvetica -18 bold',bg=g.cns['gold']).place(x=90, y=210)
Entry(w1.top, textvariable=username,font = 'Helvetica -18 bold').place(x=210, y=210)
username.set(g.user) 
Label(w1.top, text = '聚寬密碼: ',font = 'Helvetica -18 bold',bg=g.cns['gold']).place(x=90, y=250)
pas=Entry(w1.top, textvariable=password, show='*',font = 'Helvetica -18 bold')
pas.place(x=210, y=250)
pas.bind('<Key-Return>', rtnkey)   
Button(w1.top, text='登  陸', command=loginCheck,font = 'Helvetica -18 bold',bg=g.cns['gold'],fg='blue').place(x=90, y=290)
Button(w1.top, text='退  出', command=w1.destroy,font = 'Helvetica -18 bold',bg=g.cns['gold'],fg='blue').place(x=350, y=290)
setCenter(w1.top,tk_image.width()+4,tk_image.height()+4)
reSizable(w1.top,False, False)
robot_init()
MainPagejq(g.root)  

完整代碼見《零基礎搭建量化投資系統――以Python爲工具》一書提供的完整示例。
該書在京東、淘寶、噹噹網上銷售,歡迎大家訂購正版書。
在這裏插入圖片描述
《零基礎搭建量化投資系統――以Python爲工具》第8章目錄

第8章 視窗程序開發模塊Tkinter
    8.1 Tkinter的使用
    8.2 小部件的標準屬性
	8.2.1 Tkinter控件的屬性
	8.2.2 Tkinter控件屬性的值和單位
 	    1. 維度單位
 	    2. 座標系統
 	    3. 顏色
 	    4. 字體類型
 	    5. 對齊方式
 	    6. 浮雕風格
 	    7. 位圖
 	    8. 鼠標樣式
 	    9. 圖片
    8.3 Tkinter主窗口
    8.4 Toplevel頂層子窗口
    8.5 創建窗口菜單
    8.6 創建彈出菜單
    8.7 控件的幾何佈局管理方法
        1. 控件的pack佈局
        2. 控件的grid佈局
	3. 控件的place佈局
    8.8 Tkinter常用控件
        1. Label控件
        2. Frame控件
        3. Button控件
        4. Entry控件
        5. 工具條和狀態欄
        6. Text控件
        7. Scrollbar控件
        8. Canvas控件
    8.9 Tkinter的事件
        1. 事件名稱
        2. 事件類型
        3. 事件修飾符
        4. 編寫事件處理程序
    8.10 Tkinter的主題控件Ttk
        1. ttk.Notebook控件
        2. ttk.Treeview控件
    8.11 Tkinter的擴展控件Tix
        1. 使用Tix
        2. 表格幾何管理器Form
        3. tix.Balloon氣球窗口小部件
        4. tix.ButtonBox按鈕框
        5. tix.ComboBox組合框控件
        6. tix.Control控制窗口

我們希望其它風格的窗口軟件界面,這就涉及到軟件皮膚設計的問題,下面我們給出2個簡單的例子,希望拋磚引玉。
在這裏插入圖片描述
這個界面的代碼如下。

# -*- coding: utf-8 -*-
import  tkinter.ttk  as  ttk   #導入Tkinter.ttk
from PIL import Image, ImageTk
import  HP_tk  as  htk   #導入htk

root=htk.MainWindow(title='tkinter主窗口',x=0,y=0,w=800, h=577,picture='img/2.jpg',zoom=True,center=True)
root.attributes('-topmost',1)  #參數1,設置頂層窗口,覆蓋其它窗口。
root.overrideredirect(True)
png1= ImageTk.PhotoImage(Image.open('img/ska1.png'))
png2= ImageTk.PhotoImage(Image.open('img/ska2.png'))
png3= ImageTk.PhotoImage(Image.open('img/ska3.png'))

def Destroy():
    root.destroy()

bt1=ttk.Button(root, width=12, image=png1, command=root.Destroy,cursor='hand2',style='Btn.TButton')
bt2=ttk.Button(root, width=12, image=png2, command=root.setmin,cursor='hand2',style='Btn.TButton')
bt3=ttk.Button(root, width=12, image=png3, command=root.setmax,cursor='hand2',style='Btn.TButton')
bt1.place(x=14,y=544)
bt2.place(x=40,y=544)
bt3.place(x=66,y=544)

root.mainloop()

下面再看一個演示。這裏子可以放大或縮小用戶窗口。
在這裏插入圖片描述
(正常窗口)

在這裏插入圖片描述
(最小化窗口)
在這裏插入圖片描述
(最大化全屏窗口)

# -*- coding: utf-8 -*-
import  tkinter  as  tk   #導入Tkinter
import  tkinter.ttk  as  ttk   #導入Tkinter.ttk
from PIL import Image, ImageTk
import  HP_tk  as  htk   #導入htk

class skin(tk.Frame): # 繼承Frame類  
    def __init__(self, root=None):  
        self.staut=True
        self.minstaut=False
        self.width=root.winfo_width()   #獲取窗口寬度(單位:像素)
        self.height=root.winfo_height()  #獲取窗口高度(單位:像素)
        tk.Frame.__init__(self, root)  
        self.root = root #定義內部變量root  
        self.s = ttk.Style()
        self.s.configure('.', background='orange', border=100)
        self.s.configure('App.TFrame', background='yellow')
        self.s.configure('Btn.TButton', background='light blue', border=10)
        
        self.v1=tk.Frame(self,background='light blue')
        self.v1.pack(side=tk.TOP, fill=tk.X, expand=1,ipady=1,pady=1,ipadx=1,padx=1)
        
        self.img0=Image.open('ico/kchart.ico')
        self.img1=Image.open('ico/katomic.ico')
        self.img2=Image.open('ico/kpersonalizer.ico')
        self.img3=Image.open('ico/windowlist.ico')
        self.img4=Image.open('ico/exit.ico')
        
        self.img0=htk.imgresize2(48,48,self.img0)
        self.img1=htk.imgresize2(48,48,self.img1)
        self.img2=htk.imgresize2(48,48,self.img2)
        self.img3=htk.imgresize2(48,48,self.img3)
        self.img4=htk.imgresize2(48,48,self.img4)
                
        self.png0= ImageTk.PhotoImage(self.img0)
        self.png1= ImageTk.PhotoImage(self.img1)
        self.png2= ImageTk.PhotoImage(self.img2)
        self.png3= ImageTk.PhotoImage(self.img3)
        self.png4= ImageTk.PhotoImage(self.img4)
        
        
        self.t0 = ttk.Button(self.v1, width=10, image=self.png0, command=self.Destroy,cursor='hand2',style='Btn.TButton')
        self.t1 = ttk.Button(self.v1, width=10, image=self.png1, command=self.Destroy,cursor='hand2',style='Btn.TButton')
        self.t2 = ttk.Button(self.v1, width=10, image=self.png2, command=self.setmin,cursor='hand2',style='Btn.TButton')
        self.t3 = ttk.Button(self.v1, width=10, image=self.png3, command=self.setmax,cursor='hand2',style='Btn.TButton')
        self.t4 = ttk.Button(self.v1, width=10, image=self.png4, command=self.Destroy,cursor='hand2',style='Btn.TButton')
        self.t0.pack(side=tk.LEFT)
        self.t4.pack(side=tk.RIGHT)
        self.t3.pack(side=tk.RIGHT)
        self.t2.pack(side=tk.RIGHT)
        self.t1.pack(side=tk.RIGHT)
        self.pack(side=tk.TOP,fill=tk.X)
        self.Overturn()

    def Destroy(self):
        self.root.destroy()
    
    def Overturn(self):
        self.root.attributes('-topmost',1)  #參數1,設置頂層窗口,覆蓋其它窗口。
        self.root.overrideredirect(True)
    
    def setmin(self):
        if self.minstaut:
            self.root.geometry('{}x{}'.format(self.width,self.height))  #改變窗口大小
            self.minstaut=False
        else:
            self.root.geometry('{}x{}'.format(150,30))  #改變窗口大小
            self.minstaut=True
    
    def setmax(self):
        if self.staut:
            self.root.state("zoomed")  #窗口最大化。
            self.staut=False
        else:
            self.root.state("normal")  #設置爲普通窗口。
            self.staut=True
            



root=htk.MainWindow(title='tkinter主窗口',x=0,y=0,w=800, h=600,zoom=False,center=True)
sk=skin(root)
maimframe=tk.Frame(root,bg='#1E488F',bd=2)
maimframe.pack(fill=tk.BOTH, expand=tk.YES)

status=htk.StatusBar(maimframe)    #建立狀態欄
status.pack(side=tk.BOTTOM, fill=tk.X) #把狀態欄放到窗口底部
status.clear()
status.text(0,'狀態欄') #在狀態欄1輸出信息
status.text(1,'超越自我!') #在狀態欄2輸出信息
status.text(2,'超越!是我們的每一步!')
status.text(3,'版權所有')
status.text(4,'侵權必究')
status.text(5,'設計:小白')
status.config(1,color='red') #改變狀態欄2信息顏色
status.config(3,color='green') #改變狀態欄2信息顏色
status.config(4,color='blue') #改變狀態欄2信息顏色

root.mainloop()

對以上代碼有興趣的朋友可以購買《零基礎搭建量化投資系統――以Python爲工具》書,加入讀者羣,下載全部資源和代碼。

獨狼荷蒲qq:2775205
Python Tkinter小白量化羣:524949939
電話微信:18578755056
微信公衆號:獨狼股票分析

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章