Python tkinter相关Demo演示
生活随笔
收集整理的這篇文章主要介紹了
Python tkinter相关Demo演示
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Tkinter實現透明窗口最簡單的方法
import tkinter as tkroot = tk.Tk() root.title('Tkinter Window Demo') root.geometry('600x400+50+50') root.resizable(False, False) root.attributes('-alpha', 0.5)root.mainloop()
動態更改鼠標樣式
所有ttk 小部件都有 cursor 參數,允許您在鼠標懸停它們時更改光標
import tkinter as tk from tkinter import ttk from ttkbootstrap.constants import *root = tk.Tk() root.title('Tkinter Window Demo') # root.geometry('300x300') # root.resizable(False, False) # 禁止更改窗口大小 root.attributes('-alpha', 0.7)""" cursor_name: arrow based_arrow_down based_arrow_up boat bogosity bottom_left_corner bottom_right_corner bottom_side bottom_tee box_spiral center_ptr circle clock coffee_mug cross cross_reverse crosshair diamond_cross dot dotbox double_arrow draft_large draft_small draped_box exchange fleur gobbler gumby hand1 hand2 heart icon iron_cross left_ptr left_side left_tee leftbutton ll_angle lr_angle man middlebutton mouse pencil pirate plus question_arrow right_ptr right_side right_tee rightbutton rtl_logo sailboat sb_down_arrow sb_h_double_arrow sb_left_arrow sb_right_arrow sb_up_arrow sb_v_double_arrow shuttle sizing spider spraycan star target tcross top_left_arrow top_left_corner top_right_corner top_side top_tee trek ul_angle umbrella ur_angle watch xterm X_cursor """ b1 = ttk.Button(root, text="Button 1", bootstyle=SUCCESS, cursor="watch") b1.pack(side=LEFT, padx=5, pady=10)b2 = ttk.Button(root, text="Button 2", bootstyle=(INFO, OUTLINE), cursor="trek") b2.pack(side=LEFT, padx=5, pady=10)root.mainloop()
樹狀圖分層數據
畫布事件功能
不同幀之間的切換
顏色選擇器
import tkinter as tk from tkinter import ttk from tkinter.colorchooser import askcolor from ttkbootstrap.constants import *root = tk.Tk() root.title('Tkinter Color Chooser') root.geometry('300x150')def change_color():colors = askcolor(title="Tkinter Color Chooser")root.configure(bg=colors[1])ttk.Button(root,text='Select a Color',command=change_color).pack(expand=True)root.mainloop()iShot_2022-11-12_11.35.29
tkinter線程GUI程序Demo
import tkinter as tk from tkinter import ttk from tkinter.messagebox import showerror from threading import Thread import requests from ttkbootstrap.constants import *class AsyncDownload(Thread):def __init__(self, url):super().__init__()self.html = Noneself.url = urldef run(self):response = requests.get(self.url)self.html = response.textclass App(tk.Tk):def __init__(self):super().__init__()self.title('Webpage Download')self.geometry('870x400')self.resizable(0, 0)self.create_header_frame()self.create_body_frame()self.create_footer_frame()def create_header_frame(self):self.header = ttk.Frame(self)# configure the gridself.header.columnconfigure(0, weight=1)self.header.columnconfigure(1, weight=10)self.header.columnconfigure(2, weight=1)# labelself.label = ttk.Label(self.header, text='URL')self.label.grid(column=0, row=0, sticky=tk.W)# entryself.url_var = tk.StringVar()self.url_entry = ttk.Entry(self.header,textvariable=self.url_var,width=80)self.url_entry.grid(column=1, row=0, sticky=tk.EW)# download buttonself.download_button = ttk.Button(self.header, text='Download')self.download_button['command'] = self.handle_downloadself.download_button.grid(column=2, row=0, sticky=tk.E)# attach the header frameself.header.grid(column=0, row=0, sticky=tk.NSEW, padx=10, pady=10)def handle_download(self):url = self.url_var.get()if url:self.download_button['state'] = tk.DISABLEDself.html.delete(1.0, "end")download_thread = AsyncDownload(url)download_thread.start()self.monitor(download_thread)else:showerror(title='Error',message='Please enter the URL of the webpage.')def monitor(self, thread):if thread.is_alive():# check the thread every 100msself.after(100, lambda: self.monitor(thread))else:self.html.insert(1.0, thread.html)self.download_button['state'] = tk.NORMALdef create_body_frame(self):self.body = ttk.Frame(self)# text and scrollbarself.html = tk.Text(self.body, height=20, width=92)self.html.grid(column=0, row=1)scrollbar = ttk.Scrollbar(self.body,orient='vertical',command=self.html.yview)scrollbar.grid(column=1, row=1, sticky=tk.NS)self.html['yscrollcommand'] = scrollbar.set# attach the body frameself.body.grid(column=0, row=1, sticky=tk.NSEW, padx=10, pady=10)def create_footer_frame(self):self.footer = ttk.Frame(self)# configure the gridself.footer.columnconfigure(0, weight=1)# exit buttonself.exit_button = ttk.Button(self.footer,text='Exit',command=self.destroy)self.exit_button.grid(column=0, row=0, sticky=tk.E)# attach the footer frameself.footer.grid(column=0, row=2, sticky=tk.NSEW, padx=10, pady=10)if __name__ == "__main__":app = App()app.mainloop()總結
以上是生活随笔為你收集整理的Python tkinter相关Demo演示的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++中LHS和RHS指什么
- 下一篇: [2021 icas]PPG-BASED