日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python tkinter相关Demo演示

發布時間:2023/12/14 python 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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()


動態更改鼠標樣式

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)def change_cursor(event):if event.x in range(100, 300): # 當鼠標從距離零點100 到 300 的 x 坐標上開始時,光標變為忙碌狀態。root.config(cursor="watch")else:root.config(cursor="")root.bind("<Motion>", change_cursor)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 ttkbootstrap.constants import *# create root window root = tk.Tk() root.title('Treeview Demo - Hierarchical Data') root.geometry('400x200')# configure the grid layout root.rowconfigure(0, weight=1) root.columnconfigure(0, weight=1)# create a treeview tree = ttk.Treeview(root) tree.heading('#0', text='Departments', anchor=tk.W)# adding data tree.insert('', tk.END, text='目錄', iid=0, open=False) tree.insert('', tk.END, text='主頁', iid=1, open=False) tree.insert('', tk.END, text='博客', iid=2, open=False) tree.insert('', tk.END, text='聯系', iid=3, open=False) tree.insert('', tk.END, text='幫助', iid=4, open=False)# adding children of first node tree.insert('', tk.END, text='文件夾1', iid=5, open=False) tree.insert('', tk.END, text='文件夾2', iid=6, open=False) tree.move(5, 0, 0) # 5(需要添加的子節點iid) 0(代表向iid=0的節點下添加) 0(文件索引默認0開始有五個就是 0,1,2,3,4) tree.move(6, 0, 1)tree.insert('', tk.END, text='文件1', iid=7, open=False) tree.insert('', tk.END, text='文件2', iid=8, open=False) tree.move(7, 5, 0) tree.move(8, 6, 1)# place the Treeview widget on the root window tree.grid(row=0, column=0, sticky=tk.NSEW)# run the app root.mainloop()


畫布事件功能

import tkinter as tkroot = tk.Tk() root.geometry('800x600') root.title('Canvas Demo - Binding Event')canvas = tk.Canvas(root, width=600, height=400, bg='white') canvas.pack(anchor=tk.CENTER, expand=True)python_image = tk.PhotoImage(file='暴走.gif') image_item = canvas.create_image((300, 200),image=python_image ) canvas.tag_bind(image_item,'<Button-1>',lambda e: canvas.delete(image_item) )root.mainloop()


不同幀之間的切換

import tkinter as tk from tkinter import ttk from tkinter.messagebox import showerror from ttkbootstrap.constants import *class TemperatureConverter:@staticmethoddef fahrenheit_to_celsius(f, format=True):result = (f - 32) * 5/9if format:return f'{f} Fahrenheit = {result:.2f} Celsius'return result@staticmethoddef celsius_to_fahrenheit(c, format=True):result = c * 9/5 + 32if format:return f'{c} Celsius = {result:.2f} Fahrenheit'return resultclass ConverterFrame(ttk.Frame):def __init__(self, container, unit_from, converter):super().__init__(container)self.unit_from = unit_fromself.converter = converter# field optionsoptions = {'padx': 5, 'pady': 0}# temperature labelself.temperature_label = ttk.Label(self, text=self.unit_from)self.temperature_label.grid(column=0, row=0, sticky='w', **options)# temperature entryself.temperature = tk.StringVar()self.temperature_entry = ttk.Entry(self, textvariable=self.temperature)self.temperature_entry.grid(column=1, row=0, sticky='w', **options)self.temperature_entry.focus()# buttonself.convert_button = ttk.Button(self, text='Convert')self.convert_button.grid(column=2, row=0, sticky='w', **options)self.convert_button.configure(command=self.convert)# result labelself.result_label = ttk.Label(self)self.result_label.grid(row=1, columnspan=3, **options)# add padding to the frame and show itself.grid(column=0, row=0, padx=5, pady=5, sticky="nsew")def convert(self, event=None):""" Handle button click event"""try:input_value = float(self.temperature.get())result = self.converter(input_value)self.result_label.config(text=result)except ValueError as error:showerror(title='Error', message=error)def reset(self):self.temperature_entry.delete(0, "end")self.result_label.text = ''class ControlFrame(ttk.LabelFrame):def __init__(self, container):super().__init__(container)self['text'] = 'Options'# radio buttonsself.selected_value = tk.IntVar()ttk.Radiobutton(self,text='F to C',value=0,variable=self.selected_value,command=self.change_frame).grid(column=0, row=0, padx=5, pady=5)ttk.Radiobutton(self,text='C to F',value=1,variable=self.selected_value,command=self.change_frame).grid(column=1, row=0, padx=5, pady=5)self.grid(column=0, row=1, padx=5, pady=5, sticky='ew')# initialize framesself.frames = {}self.frames[0] = ConverterFrame(container,'Fahrenheit',TemperatureConverter.fahrenheit_to_celsius)self.frames[1] = ConverterFrame(container,'Celsius',TemperatureConverter.celsius_to_fahrenheit)self.change_frame()def change_frame(self):frame = self.frames[self.selected_value.get()]frame.reset()frame.tkraise()class App(tk.Tk):def __init__(self):super().__init__()self.title('Temperature Converter')self.geometry('370x120')self.resizable(False, False)if __name__ == "__main__":app = App()ControlFrame(app)app.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演示的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。