国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Python GUI之tkinter的皮膚(ttkbootstrap)打造出你的窗口之美

這篇具有很好參考價(jià)值的文章主要介紹了Python GUI之tkinter的皮膚(ttkbootstrap)打造出你的窗口之美。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

前言

tkinter是python自帶的標(biāo)準(zhǔn)gui庫(kù),對(duì)于我們自己日常做一些小程序出來(lái)給自己使用是非常不錯(cuò)的。因?yàn)閠kinter相比較其它強(qiáng)大的gui庫(kù)(PyQT,WxPython等等)而言要簡(jiǎn)單、方便、學(xué)起來(lái)也容易得很多,基本上兩三天就能學(xué)會(huì),所以非常是nice的。但是喃,它做出來(lái)的界面,如果你是沒(méi)有一定的經(jīng)驗(yàn)與技術(shù)是做不出來(lái)好看界面的,這樣就很難受額。也會(huì)很影響我們把自己做的小程序分享給別個(gè)使用。不管怎么說(shuō)都現(xiàn)在這個(gè)時(shí)代了,要是讓別人拿個(gè)20年前的界面程序來(lái)盯著看也非常難受不是,哈哈哈?。?!熟話說(shuō),愛(ài)美之心人皆有之,那何況對(duì)于我們自己的作品而言喃,那肯定還是要追求一下完美好看,不是?
所以,ttkbootstrap(第三方庫(kù))官方就很nice啊,直接把他倆結(jié)合了起來(lái),并給它穿上了一件“皮膚”(叮:tkinter,您的外掛已到賬!)。簡(jiǎn)單來(lái)說(shuō)就是會(huì)tkinter了,那么也就基本上也會(huì)ttkbootstrap了,如果不會(huì),那直接學(xué)ttkbootstrap難度也與tkinter差不多,簡(jiǎn)直就是太nice了,哈哈哈。
那么,之所以推出ttkbootstrap出來(lái)當(dāng)然是為了解決界面好看的問(wèn)題,它自己封裝了很多主題來(lái)供用戶選擇,里面的主題切換出來(lái)的效果也很現(xiàn)代化好看。所以,我們要學(xué)簡(jiǎn)單的gui庫(kù)那么就學(xué)ttkbootstrap吧?。?!好了,接下來(lái)我們就開(kāi)始對(duì)它做一些簡(jiǎn)單的使用了解吧!對(duì)了,學(xué)習(xí)ttkbootstrap的小伙伴們一定要去官方文檔里面的查看內(nèi)容,里面也提供了好幾個(gè)項(xiàng)目來(lái)供大家參考!
(提示:本文是考慮在對(duì)tkinter有一定了解的基礎(chǔ)上與大家分享學(xué)習(xí)的,因?yàn)橄旅娴膬?nèi)容大多數(shù)都沒(méi)注釋,所以沒(méi)有基礎(chǔ)的小伙伴還請(qǐng)見(jiàn)諒!)

官方文檔:https://ttkbootstrap.readthedocs.io/en/latest/

這兩張圖片是截取官網(wǎng)上提供的展示效果圖。
tkinter漂亮gui界面模板,GUI,python
tkinter漂亮gui界面模板,GUI,python

主題切換

簡(jiǎn)單的主題切換,由于當(dāng)前窗口上組件很少,所以感覺(jué)效果不明顯,但是當(dāng)組件布局很多時(shí)就會(huì)很好看。
tkinter漂亮gui界面模板,GUI,python

import ttkbootstrap as ttk
from ttkbootstrap.constants import *
root = ttk.Window()
style = ttk.Style()
theme_names = style.theme_names()#以列表的形式返回多個(gè)主題名
theme_selection = ttk.Frame(root, padding=(10, 10, 10, 0))
theme_selection.pack(fill=X, expand=YES)
lbl = ttk.Label(theme_selection, text="選擇主題:")
theme_cbo = ttk.Combobox(
        master=theme_selection,
        text=style.theme.name,
        values=theme_names,
)
theme_cbo.pack(padx=10, side=RIGHT)
theme_cbo.current(theme_names.index(style.theme.name))
lbl.pack(side=RIGHT)
def change_theme(event):
    theme_cbo_value = theme_cbo.get()
    style.theme_use(theme_cbo_value)
    theme_selected.configure(text=theme_cbo_value)
    theme_cbo.selection_clear()
theme_cbo.bind('<<ComboboxSelected>>', change_theme)
theme_selected = ttk.Label(
        master=theme_selection,
        text="litera",
        font="-size 24 -weight bold"
)
theme_selected.pack(side=LEFT)
root.mainloop()

ttkbootstrap一些簡(jiǎn)單使用介紹

首先對(duì)它實(shí)例化創(chuàng)建應(yīng)用程序窗口的一些簡(jiǎn)單介紹。

import ttkbootstrap as ttk
#實(shí)例化創(chuàng)建應(yīng)用程序窗口
root = ttk.Window(
        title="窗口名字",        #設(shè)置窗口的標(biāo)題
        themename="litera",     #設(shè)置主題
        size=(1066,600),        #窗口的大小
        position=(100,100),     #窗口所在的位置
        minsize=(0,0),          #窗口的最小寬高
        maxsize=(1920,1080),    #窗口的最大寬高
        resizable=None,         #設(shè)置窗口是否可以更改大小
        alpha=1.0,              #設(shè)置窗口的透明度(0.0完全透明)
        )
# root.place_window_center()    #讓顯現(xiàn)出的窗口居中
# root.resizable(False,False)   #讓窗口不可更改大小
# root.wm_attributes('-topmost', 1)#讓窗口位置其它窗口之上
root.mainloop()

標(biāo)簽

tkinter漂亮gui界面模板,GUI,python

import ttkbootstrap as ttk
from ttkbootstrap.constants import *
root = ttk.Window()
ttk.Label(root,text="標(biāo)簽1",bootstyle=INFO).pack(side=ttk.LEFT, padx=5, pady=10)
ttk.Label(root,text="標(biāo)簽2",bootstyle="inverse").pack(side=ttk.LEFT, padx=5, pady=10)
ttk.Label(root,text="標(biāo)簽3",bootstyle="inverse-danger").pack(side=ttk.LEFT, padx=5, pady=10)
ttk.Label(root, text="標(biāo)簽4", bootstyle=WARNING, font=("微軟雅黑", 15), background='#94a2a4').pack(side=LEFT, padx=5, pady=10)
root.mainloop()
'''
# bootstyle colors
PRIMARY = 'primary'
SECONDARY = 'secondary'
SUCCESS = 'success'
DANGER = 'danger'
WARNING = 'warning'
INFO = 'info'
LIGHT = 'light'
DARK = 'dark'

# bootstyle types
OUTLINE = 'outline'
LINK = 'link'
TOGGLE = 'toggle'
INVERSE = 'inverse'
STRIPED = 'striped'
TOOLBUTTON = 'toolbutton'
ROUND = 'round'
SQUARE = 'square'
'''

按鈕

按鈕樣式
tkinter漂亮gui界面模板,GUI,python

import ttkbootstrap as ttk
from ttkbootstrap.constants import *
root = ttk.Window()
ttk.Button(root, text="Button 1", bootstyle=SUCCESS).pack(side=LEFT, padx=5, pady=10)
ttk.Button(root, text="Button 2", bootstyle=(INFO, OUTLINE)).pack(side=LEFT, padx=5, pady=10)
ttk.Button(root, text="Button 3", bootstyle=(PRIMARY, "outline-toolbutton")).pack(side=LEFT, padx=5, pady=10)
ttk.Button(root, text="Button 4", bootstyle="link").pack(side=LEFT, padx=5, pady=10)
ttk.Button(root, text="Button 5", bootstyle="success-link").pack(side=LEFT, padx=5, pady=10)
ttk.Button(root, text="Button 6", state="disabled").pack(side=LEFT, padx=5, pady=10) #在禁用狀態(tài)下創(chuàng)建按鈕
root.mainloop()

按鈕點(diǎn)擊
tkinter漂亮gui界面模板,GUI,python

import ttkbootstrap as ttk
from ttkbootstrap.constants import *
root = ttk.Window()
#為按鈕添加點(diǎn)擊事件
#法一
def button1():
    print("Button1點(diǎn)擊了一下!")
ttk.Button(root,text="Button1", bootstyle=(PRIMARY, "outline-toolbutton"),command=button1).pack(side=LEFT, padx=5, pady=10)
#法二
def button2(event): #這里要加一個(gè)參數(shù),不然會(huì)報(bào)錯(cuò)
    print("Button2點(diǎn)擊了一下!")
    button_text = event.widget["text"] #得到按鈕上的文本
    print(button_text)
b = ttk.Button(root,text="Button2", bootstyle=(PRIMARY, "outline-toolbutton"))
b.pack(side=LEFT, padx=5, pady=10)
b.bind("<Button-1>", button2) #<Button-1>鼠標(biāo)左鍵
root.mainloop()

輸入框

tkinter漂亮gui界面模板,GUI,python

import ttkbootstrap as ttk
from ttkbootstrap.constants import *
root = ttk.Window()
e1 = ttk.Entry(root,show=None)
e1.insert('0',"默認(rèn)插入內(nèi)容")
e1.grid(row=5, column=1, sticky=ttk.W, padx=10,pady=10)
e2 = ttk.Entry(root,show="*",width=50,bootstyle=PRIMARY)
e2.grid(row=10, column=1, sticky=ttk.W, padx=10, pady=10)
e3_content = ttk.StringVar()
e3 = ttk.Entry(root,bootstyle='success', textvariable=e3_content).grid(row=15, column=1, sticky=ttk.W, padx=10, pady=10)
def get_entry_contetn():
    print("e1: ",e1.get())
    print("e2: ",e2.get())
    print("e3: ",e3_content.get())
ttk.Button(root,text="get_entry_contetn", bootstyle=(PRIMARY, "outline-toolbutton"),command=get_entry_contetn).grid(row=20, column=1, sticky=ttk.W, padx=10, pady=10)
root.mainloop()

文本框

tkinter漂亮gui界面模板,GUI,python

import ttkbootstrap as ttk
from ttkbootstrap.constants import *
root = ttk.Window()
text = ttk.Text(root,)
text.pack(padx=10,pady=10,fill=BOTH)
text.insert('insert','text-content 1') #插入內(nèi)容
text.delete("0.0",'end') #刪除內(nèi)容
text.insert('insert','text-content 2\npy')
text.see(ttk.END) #光標(biāo)跟隨著插入的內(nèi)容移動(dòng)
root.mainloop()

日期輸入

tkinter漂亮gui界面模板,GUI,python

import ttkbootstrap as ttk
from ttkbootstrap.constants import *
root = ttk.Window()
de1 = ttk.DateEntry()
de1.grid(row=6, column=1, sticky=ttk.W,padx=10, pady=10)
print(de1.entry.get())

de2 = ttk.DateEntry(bootstyle="success",dateformat=r"%Y") #r"%Y-%m-%d"
de2.grid(row=6, column=2, sticky=ttk.W,padx=10, pady=10)
def get_dataentry():
    print(de2.entry.get())
ttk.Button(root,text="get_dataentry", bootstyle=(PRIMARY, "outline-toolbutton"),command=get_dataentry).grid(row=20, column=1, sticky=ttk.W, padx=10, pady=10)
root.mainloop()

單選按鈕

tkinter漂亮gui界面模板,GUI,python

import ttkbootstrap as ttk
root = ttk.Window()
variable_value = ttk.StringVar()
variable_value_dist = {
    "0":"男",
    "1":"女",
    "2":"未知"
}
ttk.Radiobutton(root, text='男', variable=variable_value, value=0).pack(side=ttk.LEFT, padx=5)
ttk.Radiobutton(root, text='女', variable=variable_value, value=1).pack(side=ttk.LEFT, padx=5)
ttk.Radiobutton(root, text='未知', variable=variable_value, value=2).pack(side=ttk.LEFT, padx=5)
def ensure():
    print(variable_value_dist[variable_value.get()])
ttk.Button(text="確定", command=ensure).pack(side=ttk.LEFT, padx=5)
root.mainloop()

多選按鈕

tkinter漂亮gui界面模板,GUI,python

import ttkbootstrap as ttk
root = ttk.Window()
variable_content = [
    [ttk.StringVar(),"111"],
    [ttk.StringVar(),"222"],
    [ttk.StringVar(),"333"],
    [ttk.StringVar(),"666"]
]
ttk.Checkbutton(root, text="111", variable=variable_content[0][0]).pack(side=ttk.LEFT, padx=5)
ttk.Checkbutton(root, text="222", variable=variable_content[1][0], bootstyle="square-toggle").pack(side=ttk.LEFT, padx=5)
ttk.Checkbutton(root, text="333", variable=variable_content[2][0], bootstyle="round-toggle").pack(side=ttk.LEFT, padx=5)
ttk.Checkbutton(root, text="666", variable=variable_content[3][0]).pack(side=ttk.LEFT, padx=5)
def ensure():
    print([v for i, v in variable_content if i.get()])
ttk.Button(text="確定",command=ensure).pack(side=ttk.LEFT, padx=5)
root.mainloop()

組合框

tkinter漂亮gui界面模板,GUI,python

import ttkbootstrap as ttk
from ttkbootstrap.constants import *
root = ttk.Window()
cbo = ttk.Combobox(
            master=root,
            bootstyle = DANGER,
            font = ("微軟雅黑",12),
            values=['content 1', 'content 2', 'content 3'],
        )
cbo.current(1) #首先展示values里面索引的對(duì)應(yīng)的值
cbo.pack()
# cbo.set('set other')
def ensure(event):
    print(cbo.get())
cbo.bind('<<ComboboxSelected>>', ensure)
root.mainloop()

Frame和Labelframe

tkinter漂亮gui界面模板,GUI,python

import ttkbootstrap as ttk
from ttkbootstrap.constants import *
root = ttk.Window()
f = ttk.Frame(bootstyle=SUCCESS)
f.place(x=10,y=10,width=600,height=200)

lf = ttk.Labelframe(text="提示",bootstyle=PRIMARY,width=100,height=60)
lf.place(x=10,y=210,width=300,height=100)
ttk.Label(lf,text="標(biāo)簽").pack()
ttk.Button(lf,text="按鈕").pack()
root.mainloop()

儀表

tkinter漂亮gui界面模板,GUI,python

import psutil,time,threading
import ttkbootstrap as ttk
from ttkbootstrap.constants import *
root = ttk.Window()
ttk.Meter(
        master=root,
        bootstyle=DEFAULT,
        metertype="full",#將儀表顯示為一個(gè)完整的圓形或半圓形(semi)
        wedgesize=5, #設(shè)置弧周圍的指示器楔形長(zhǎng)度,如果大于 0,則此楔形設(shè)置為以當(dāng)前儀表值為中心的指示器
        amounttotal=50, #儀表的最大值,默認(rèn)100
        amountused=10, #儀表的當(dāng)前值
        metersize=200,#儀表大小
        showtext=True, #指示是否在儀表上顯示左、中、右文本標(biāo)簽
        interactive=True, #是否可以手動(dòng)調(diào)節(jié)數(shù)字的大小
        textleft='左邊', #插入到中心文本左側(cè)的短字符串
        textright='右邊',
        textfont="-size 30", #中間數(shù)字大小
        subtext="文本",
        subtextstyle=DEFAULT,
        subtextfont="-size 20",#文本大小
        ).pack(side=ttk.LEFT, padx=5)
def _():
        meter = ttk.Meter(
                metersize=180,
                padding=50,
                amountused=0,
                metertype="semi",
                subtext="當(dāng)前網(wǎng)速(kB/s)",
                subtextstyle="warning",
                interactive=False,
                bootstyle='primary',
                )
        meter.pack(side=ttk.LEFT, padx=5)
        while True:
                meter.configure(amountused=round(getNet(),2))
def getNet():
    recv_before = psutil.net_io_counters().bytes_recv
    time.sleep(1)
    recv_now = psutil.net_io_counters().bytes_recv
    recv = (recv_now - recv_before)/1024
    return recv

t = threading.Thread(target=_)
t.setDaemon(True)
t.start()
root.mainloop()

進(jìn)度條

tkinter漂亮gui界面模板,GUI,python

import time,threading
import ttkbootstrap as ttk
from ttkbootstrap.constants import *
root = ttk.Window(size=(500,380))
def _():
    f = ttk.Frame(root).pack(fill=BOTH, expand=YES)
    p1 = ttk.Progressbar(f, bootstyle=PRIMARY)
    p1.place(x=20, y=20, width=380, height=40)
    p1.start() #間隔默認(rèn)為50毫秒(20步/秒)
    p2 = ttk.Progressbar(f, bootstyle=INFO,orient=VERTICAL)
    p2.place(x=200, y=100, width=40, height=200)
    p2.step(10) #步長(zhǎng)
    while True:
        for i in range(0,50,5):
            p2.step(i) #以步長(zhǎng)i增長(zhǎng)
            # p2.stop()#停止
            time.sleep(1)
t = threading.Thread(target=_)
t.setDaemon(True)
t.start()
root.mainloop()

Scale

tkinter漂亮gui界面模板,GUI,python

import threading,time
import ttkbootstrap as ttk
from ttkbootstrap.constants import *
root = ttk.Window()
ttk.Scale(
    master=root,
    orient=HORIZONTAL,
    value=75,
    from_=0,
    to=100
).pack(fill=X, pady=5, expand=YES)
ttk.Scale(master=root,orient=HORIZONTAL,bootstyle=WARNING,value=75,from_=100,to=0).pack(fill=X, pady=5, expand=YES)
def scale():
    s2 = ttk.Scale(
        master=root,
        bootstyle=SUCCESS,
        orient=VERTICAL,
        value=0,
        from_=100,
        to=0
    )
    s2.pack(fill=X, pady=5, expand=YES)
    for i in range(101):
        s2.configure(value=i)
        time.sleep(0.1)
        # print(s2.get())
t = threading.Thread(target=scale)
t.setDaemon(True)
t.start()
root.mainloop()

水尺

tkinter漂亮gui界面模板,GUI,python

import ttkbootstrap as ttk
from ttkbootstrap.constants import *
root = ttk.Window()
fg1 = ttk.Floodgauge(
    master=None,
    cursor=None,
    font=None,
    length=None,
    maximum=100,
    mode=DETERMINATE,
    orient=HORIZONTAL,
    bootstyle=PRIMARY,
    takefocus=False,
    text=None,
    value=0,
    mask=None,
)
fg1.pack(side=ttk.LEFT, padx=5)
fg1.start()
fg2 = ttk.Floodgauge(
    master=root,
    bootstyle="success",
    font=("微軟雅黑",12), #文本字體
    length=100,  #水尺長(zhǎng)度
    maximum=10, #增加到10
    mode=INDETERMINATE, #來(lái)回不確定
    orient=VERTICAL, #放置垂直方向
    text="文本", #文本
)
fg2.pack(side=ttk.LEFT, padx=5)
fg2.start()
fg3 = ttk.Floodgauge(
    root,
    bootstyle=INFO,
    length=300,
    maximum=200,
    font=("微軟雅黑", 18, 'bold'),
    mask='loading...{}%',
)
fg3.pack(side=ttk.LEFT, padx=5)
fg3.start()
# fg3.stop()
# fg3.configure(mask='...{}%')
fg3.configure(value=25) #初始值
fg3.step(50) #將上面25該值增加50步
root.mainloop()

滾動(dòng)條

tkinter漂亮gui界面模板,GUI,python

import ttkbootstrap as ttk
from ttkbootstrap.constants import *
root = ttk.Window(size=(500,200))
f = ttk.Frame(root).pack(fill=BOTH, expand=YES)
text_content = '''
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
'''
# t = ttk.Text(f)
# t.insert("0.0",text_content)
# t.place(x=10,y=10,width=480,height=200)
# sl_x = ttk.Scrollbar(t,orient=HORIZONTAL) #使?jié)L動(dòng)條水平放置
# #放到窗口的底部, 填充X軸豎直方向
# sl_x.pack(side=ttk.BOTTOM, fill=ttk.X)
# sl_y = ttk.Scrollbar(t,bootstyle="round-success") #滾動(dòng)條默認(rèn)垂直放置
# #放到窗口的右側(cè), 填充Y軸豎直方向
# sl_y.pack(side=ttk.RIGHT, fill=ttk.Y)
# #兩個(gè)控件相關(guān)聯(lián)
# sl_x.config(command=t.xview)
# t.config(yscrollcommand=sl_x.set)
# sl_y.config(command=t.yview)
# t.config(yscrollcommand=sl_y.set)

##滾動(dòng)文本框
from ttkbootstrap.scrolled import ScrolledText
st = ScrolledText(f, padding=5, height=10, autohide=True)
st.pack(fill=BOTH, expand=YES)
st.insert(END, text_content)

root.mainloop()

消息提示框

tkinter漂亮gui界面模板,GUI,python

import ttkbootstrap as ttk
from ttkbootstrap.dialogs import Messagebox
root = ttk.Window()
print("ok: ",Messagebox.ok(
    message="要在消息框中顯示的消息",
    title="消息框的標(biāo)題",
    alert=False, #指定是否響鈴,默認(rèn)False
))
print("okcancel: ",Messagebox.okcancel(message="確定取消"))
print("retrycancel: ",Messagebox.retrycancel(message="重試取消"))
print("retrycancel: ",Messagebox.show_error(message="顯示錯(cuò)誤"))
print("retrycancel: ",Messagebox.show_info(message="顯示信息"))
print("retrycancel: ",Messagebox.show_question(message="顯示問(wèn)題"))
print("retrycancel: ",Messagebox.show_warning(message="顯示警告"))
print("retrycancel: ",Messagebox.yesno(message="是的"))
print("retrycancel: ",Messagebox.yesnocancel(message="是的取消"))
root.mainloop()

查詢框

tkinter漂亮gui界面模板,GUI,python

import ttkbootstrap as ttk
from ttkbootstrap.dialogs import Querybox
root = ttk.Window()
print("獲取日期:",Querybox.get_date())
print("獲取float型:",Querybox.get_float(
    prompt="請(qǐng)輸入內(nèi)容:",
    title="獲取float型: ",
    initialvalue=666.666, #設(shè)置初始值
    # minvalue=None, 
    # maxvalue=None
))
print("獲取字體:",Querybox.get_font())
print("獲取整數(shù):",Querybox.get_integer())
print("獲取字符串:",Querybox.get_string())
root.mainloop()

子窗口

tkinter漂亮gui界面模板,GUI,python

import ttkbootstrap as ttk
root = ttk.Window()
root.wm_attributes('-topmost', 1)#讓主窗口置頂
def my():
    ttk.Style("solar")
    #print(ttk.Style().theme_names())#可設(shè)置主題風(fēng)格['cyborg', 'journal', 'darkly', 'flatly', 'solar', 'minty', 'litera', 'united', 'pulse', 'cosmo', 'lumen', 'yeti', 'superhero']
    mytoplevel = ttk.Toplevel(root,alpha=0.5)##里面的參數(shù)和Window()父窗口一致
ttk.Button(text="my_Toplevel ",command=my).pack()
root.mainloop()

菜單

新增,之前總是感覺(jué)好像缺了點(diǎn)什么,今天才想起來(lái),哈哈哈?。?!
tkinter漂亮gui界面模板,GUI,python

import ttkbootstrap as ttk
from ttkbootstrap.dialogs import Messagebox
root = ttk.Window()
# 在窗口上創(chuàng)建一個(gè)菜單欄(最上方的菜單欄橫條)
menubar = ttk.Menu(root)
def dianji(): #定義一個(gè)點(diǎn)擊事件方法
    Messagebox.show_info(title='走走走!', message='該功能還未開(kāi)放!')   #消息提示框窗口
# 定義一個(gè)豎條
filemenu = ttk.Menu(menubar)
# 在菜單單元中添加一個(gè)菜單項(xiàng)File
menubar.add_cascade(label='設(shè)置', menu=filemenu)
# 在設(shè)置菜單項(xiàng)添加命令選項(xiàng)
filemenu.add_command(label='更多', command=dianji)
# 添加一條分割線
filemenu.add_separator()
# 定義一個(gè)子菜單條
submenu = ttk.Menu(filemenu)  # 和上面定義菜單一樣,不過(guò)此處是在設(shè)置上創(chuàng)建一個(gè)空的菜單
submenu.add_command(label="背景")  # 給submenu添加功能選項(xiàng)
submenu.add_command(label="字體")
submenu.add_command(label="大小")
# 添加一個(gè)展開(kāi)下拉菜單,并把上面的子菜單嵌入給它
filemenu.add_cascade(label='個(gè)性化', menu=submenu, underline=0)
# 同樣的在File中加入Exit小菜單,此處對(duì)應(yīng)命令為window.quit
filemenu.add_command(label='退出', command=root.quit)
# 在頂部再添加多個(gè)菜單項(xiàng)
otherfunction = ttk.Menu(menubar)
menubar.add_cascade(label='選擇', menu=otherfunction)
menubar.add_cascade(label='查看', menu=otherfunction)
otherfunction.add_command(label='該功能未開(kāi)放!')
#幫助
def baidu():
    Messagebox.okcancel(title='baidu.com', message='走走走,自己去百度!')
help = ttk.Menu(menubar, tearoff=0)
menubar.add_cascade(label='幫助', menu=help)
help.add_command(label='help', command=baidu)
# 將菜單配置給窗口
root.config(menu=menubar)

root.mainloop()

面板

tkinter漂亮gui界面模板,GUI,python

import ttkbootstrap as ttk
from ttkbootstrap.constants import *
root = ttk.Window()
f = ttk.Frame(root)
f.pack(pady=5, fill=X, side=TOP)
nb = ttk.Notebook(f)
nb.pack(
    side=LEFT,
    padx=(10, 0),
    expand=YES,
    fill=BOTH
)
nb_text = "This is a notebook tab.\nYou can put any widget you want here."
nb.add(ttk.Label(nb, text=nb_text), text="Tab 1", sticky=NW)
nb.add(
    child=ttk.Label(nb, text="notebook tab 2."),
    text="Tab 2",
    sticky=NW
)
f2 = ttk.Frame(nb)
ttk.Button(f2,text="notebook button").pack(side=ttk.LEFT, padx=5, pady=10)
nb.add(f2, text='Tab 3')
root.mainloop()

樹(shù)視圖

tkinter漂亮gui界面模板,GUI,python

import ttkbootstrap as ttk
from ttkbootstrap.constants import *
root = ttk.Window()
tv = ttk.Treeview(
        master=root,
        columns=[0, 1],
        show=HEADINGS,
        height=5
    )
table_data = [
    (1,'one'),
    (2, 'two'),
    (3, 'three'),
    (4, 'four'),
    (5, 'five')
]
for row in table_data:
    tv.insert('', END, values=row)
# print(tv.get_children())#('I001', 'I002', 'I003', 'I004', 'I005')
tv.selection_set('I002')
tv.heading(0, text='ID')
tv.heading(1, text='NAME')
tv.column(0, width=60)
tv.column(1, width=300, anchor=CENTER)
tv.pack(side=LEFT, anchor=NE, fill=X)
root.mainloop()

加載gif動(dòng)圖

左邊是官網(wǎng)上提供的方法,右邊是一個(gè)自己定義的方法。
tkinter漂亮gui界面模板,GUI,python

from pathlib import Path
from itertools import cycle
import ttkbootstrap as ttk
from ttkbootstrap.constants import *
from PIL import Image, ImageTk, ImageSequence
class AnimatedGif(ttk.Frame):
    def __init__(self, master):
        super().__init__(master, width=400, height=300)

        # open the GIF and create a cycle iterator
        file_path = Path(__file__).parent / "guanwang.gif"
        with Image.open(file_path) as im:
            # create a sequence
            sequence = ImageSequence.Iterator(im)
            images = [ImageTk.PhotoImage(s) for s in sequence]
            self.image_cycle = cycle(images)

            # length of each frame
            self.framerate = im.info["duration"]

        self.img_container = ttk.Label(self, image=next(self.image_cycle))
        self.img_container.pack(fill="both", expand="yes")
        self.after(self.framerate, self.next_frame)

    def next_frame(self):
        """Update the image for each frame"""
        self.img_container.configure(image=next(self.image_cycle))
        self.after(self.framerate, self.next_frame)

def loadingGif(app):
    numIdx = 12  # gif的幀數(shù)
    file_path = Path(__file__).parent / "TestGif.gif"
    frames = [ttk.PhotoImage(file=file_path, format='gif -index %i' % (i)) for i in range(numIdx)]
    def run(rate):
        frame = frames[rate]
        rate += 1
        gif_label.configure(image=frame)  # 顯示當(dāng)前幀的圖片
        gif_label.after(100, run, rate % numIdx)  # 0.1秒(100毫秒)之后繼續(xù)執(zhí)行函數(shù)(run)
    gif_label = ttk.Label(app)
    gif_label.pack(side=LEFT,padx=20,fill=BOTH, expand=YES)
    run(0)
if __name__ == "__main__":
    app = ttk.Window("Animated GIF", themename="litera")
    gif = AnimatedGif(app)
    gif.pack(side=LEFT,padx=20,fill=BOTH, expand=YES)
    loadingGif(app)
    app.mainloop()

打開(kāi)本地文件

import ttkbootstrap as ttk
from ttkbootstrap.constants import *
from tkinter.filedialog import askopenfilename
root = ttk.Window()
def open_file():
    path = askopenfilename()
    # print(path)
    if not path:
        return
ttk.Button(root, text="打開(kāi)文件", command=open_file).pack(fill=X, padx=10, pady=10)
root.mainloop()

打開(kāi)瀏覽器

import ttkbootstrap as ttk
from ttkbootstrap.constants import *
import webbrowser
root = ttk.Window()
def open_url(event):
    webbrowser.open("http://www.baidu.com", new=0)  # 啟動(dòng)web瀏覽器訪問(wèn)給定的URL
label = ttk.Label(root,text="https://www.baidu.com/",bootstyle=PRIMARY)
label.pack(fill=BOTH)
label.bind("<Button-1>", open_url)
root.mainloop()

好了,分享已經(jīng)沒(méi)有了!

那么,就感謝大家查閱啦?。?!文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-781312.html

到了這里,關(guān)于Python GUI之tkinter的皮膚(ttkbootstrap)打造出你的窗口之美的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • Python制作簡(jiǎn)易計(jì)算器(GUI)---Tkinter

    Python制作簡(jiǎn)易計(jì)算器(GUI)---Tkinter

    tkinter模塊是Python標(biāo)準(zhǔn)的TK GUI工具包接口,可以實(shí)現(xiàn)一些較為簡(jiǎn)單GUI的創(chuàng)建。 麻雀雖小,五臟俱全 TK工具集使用TCL(Tool Command Language)語(yǔ)言開(kāi)發(fā) 運(yùn)行相同的程序時(shí),運(yùn)行速度快于pyqt5 組件能滿足基本使用 程序界面較簡(jiǎn)陋 開(kāi)發(fā)時(shí),使用的語(yǔ)句更短更易懂 Python內(nèi)置的標(biāo)準(zhǔn)庫(kù) Qt框

    2024年02月08日
    瀏覽(27)
  • Python的GUI編程庫(kù)Tkinter的學(xué)習(xí)

    Python的GUI編程庫(kù)Tkinter的學(xué)習(xí)

    GUI編程 就像是 搭積木 一樣,把庫(kù)里一個(gè)個(gè)的組件填好參數(shù)放到一個(gè)主體里面,最終形成一個(gè)可用的用戶界面。 總體的流程大概是如此這般: 導(dǎo)入庫(kù)- 創(chuàng)建主體對(duì)象 - 組裝庫(kù)中的組件放入到主體對(duì)象里。 Tkinter 內(nèi)置到python的安裝包里,可以不用下載直接使用。 導(dǎo)入庫(kù) 如下:

    2024年02月21日
    瀏覽(22)
  • 【Python】線程threading與GUI窗口tkinter結(jié)合應(yīng)用

    【Python】線程threading與GUI窗口tkinter結(jié)合應(yīng)用

    Python的threading模塊是一個(gè)強(qiáng)大的工具,它提供了高級(jí)別的線程編程接口。通過(guò)這個(gè)模塊,Python程序員可以在應(yīng)用程序中實(shí)現(xiàn)多線程并發(fā)執(zhí)行。 線程(Thread)是程序執(zhí)行流的最小單元,被包涵在進(jìn)程之中,是進(jìn)程中的一個(gè)實(shí)體,是被系統(tǒng)獨(dú)立調(diào)度和分派的基本單位。線程自己不

    2024年01月19日
    瀏覽(28)
  • 基于Python+Tkinter GUI 的模式識(shí)別水果分類小程序

    基于Python+Tkinter GUI 的模式識(shí)別水果分類小程序

    ? 采用Python語(yǔ)言編寫(xiě),并結(jié)合Tkinter GUI工具制作交互式小程序開(kāi)發(fā),實(shí)現(xiàn)了簡(jiǎn)單的水果的邊緣提取和分類。如圖1-A,用戶可以自定義選擇路徑并輸出,同時(shí)可以在對(duì)話框中輸入/輸出結(jié)果,如圖1-B。 A 界面展示 B 交互展示 ? 圖1 Tkinter GUI 展示 本次課程實(shí)踐一整體設(shè)計(jì)分為三個(gè)部

    2024年02月09日
    瀏覽(150)
  • Python創(chuàng)建一個(gè)GUI(Tkinter)選擇文件的界面獲取文件路徑

    Python創(chuàng)建一個(gè)GUI(Tkinter)選擇文件的界面獲取文件路徑

    1、點(diǎn)擊【選擇路徑可以選擇文件】 2、【確定(開(kāi)始執(zhí)行)】 這個(gè)按鈕沒(méi)有綁定函數(shù),大家可以根據(jù)自己的需求綁定函數(shù)

    2024年02月11日
    瀏覽(24)
  • Python GUI界面界面—tkinter,學(xué)習(xí)、復(fù)習(xí)、查閱,這一篇就夠了

    Python GUI界面界面—tkinter,學(xué)習(xí)、復(fù)習(xí)、查閱,這一篇就夠了

    ? ? ? ? PythonGUI程序界面設(shè)計(jì)tkinter優(yōu)點(diǎn):無(wú)需另外下載,是自帶的、操作簡(jiǎn)單、易方便; ?????????????????????????????????????????????????????????缺點(diǎn):界面控件較少,頁(yè)面不夠美觀 注:一般來(lái)說(shuō)做小軟件tkinter足夠了(頁(yè)面可以用ttkbootstrap美化

    2024年01月17日
    瀏覽(20)
  • 《零基礎(chǔ)入門(mén)學(xué)習(xí)Python》第073講:GUI的終極選擇:Tkinter10

    《零基礎(chǔ)入門(mén)學(xué)習(xí)Python》第073講:GUI的終極選擇:Tkinter10

    我們不難發(fā)現(xiàn),幾乎每一個(gè)應(yīng)用程序都有一些相同的地方,比如說(shuō):標(biāo)題欄、狀態(tài)欄、邊框、滾動(dòng)條、工作區(qū)。還有的就是 菜單。 傳統(tǒng)的菜單有大家熟悉的 File,Edit,Help等,點(diǎn)開(kāi)之后,是下拉菜單,今天我們就來(lái)學(xué)習(xí)使用 tkinter 如何制作這樣子的菜單。tkinter 提供了一個(gè)叫

    2024年02月15日
    瀏覽(24)
  • Python進(jìn)階知識(shí)(2)—— 什么是GUI編程?一起來(lái)學(xué)習(xí)用Python,Tkinter“做畫(huà)”吧

    Python進(jìn)階知識(shí)(2)—— 什么是GUI編程?一起來(lái)學(xué)習(xí)用Python,Tkinter“做畫(huà)”吧

    All things are difficult before they are easy. 凡事必先難后易。 GUI是Graphical User Interface(圖形用戶界面)的縮寫(xiě) 。它是指通過(guò)圖形化的方式,為人們提供更加友好、直觀的用戶界面,使得用戶可以通過(guò)鼠標(biāo)和鍵盤(pán)等外設(shè)更加輕松、快捷地操作計(jì)算機(jī)。在GUI界面中,計(jì)算機(jī)會(huì)呈現(xiàn)出各種

    2024年02月05日
    瀏覽(39)
  • 100個(gè)Python實(shí)戰(zhàn)項(xiàng)目(十三)使用 Tkinter GUI 庫(kù)構(gòu)建鬧鐘應(yīng)用程序

    100個(gè)Python實(shí)戰(zhàn)項(xiàng)目(十三)使用 Tkinter GUI 庫(kù)構(gòu)建鬧鐘應(yīng)用程序

    本文主要是使用 Python 的 Tkinter 庫(kù)創(chuàng)建一個(gè)簡(jiǎn)單的鬧鐘程序,它可以在指定的時(shí)間播放一個(gè)聲音來(lái)提醒你。這個(gè)程序界面比較簡(jiǎn)單,只有一個(gè)標(biāo)簽、三個(gè)選項(xiàng)菜單和一個(gè)按鈕,用戶可以通過(guò)選項(xiàng)菜單設(shè)置鬧鐘的時(shí)間,然后點(diǎn)擊按鈕開(kāi)始計(jì)時(shí),直到鬧鐘時(shí)間到達(dá)時(shí),程序會(huì)播放

    2023年04月16日
    瀏覽(20)
  • 《零基礎(chǔ)入門(mén)學(xué)習(xí)Python》第070講:GUI的終極選擇:Tkinter7

    《零基礎(chǔ)入門(mén)學(xué)習(xí)Python》第070講:GUI的終極選擇:Tkinter7

    上節(jié)課我們介紹了Text組件的Indexs 索引和 Marks 標(biāo)記,它們主要是用于定位,Marks 可以看做是特殊的 Indexs,但是它們又不是完全相同的,比如在默認(rèn)情況下,你在Marks指定的位置中插入數(shù)據(jù),Marks 的位置會(huì)自動(dòng)發(fā)生改變, 因?yàn)镸arks 認(rèn)它后面的“那個(gè)家伙”,當(dāng) Marks 前面的數(shù)據(jù)

    2024年02月15日
    瀏覽(18)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包