-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui-循环函数.py
105 lines (75 loc) · 2.44 KB
/
ui-循环函数.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#函数一,垃圾
# import tkinter as tk
# from tkinter import messagebox
# import time
# from watchdog.observers import Observer
# from watchdog.events import FileSystemEventHandler
# class FileChangeHandler(FileSystemEventHandler):
# def __init__(self, filename):
# self.filename = filename
# def on_modified(self, event):
# if not event.is_directory and event.src_path == self.filename:
# # 文件被修改时的处理逻辑
# with open(self.filename, 'r') as file:
# lines = file.readlines()
# if lines:
# last_line = lines[-1].strip()
# print(last_line)
# messagebox.showinfo("文件内容变化", last_line)
# def start_file_monitoring(filename):
# event_handler = FileChangeHandler(filename)
# observer = Observer()
# observer.schedule(event_handler, path='.', recursive=False)
# observer.start()
# root = tk.Tk()
# root.withdraw()
# try:
# while True:
# time.sleep(1)
# except KeyboardInterrupt:
# observer.stop()
# observer.join()
# if __name__ == '__main__':
# filename = 'file.txt' # 要监听的文件名
# start_file_monitoring(filename)
#函数二,可行
import tkinter as tk
def update_label():
# 更新标签内容
global counta
counta += 1
label.config(text="Count: {}".format(counta))
# 继续定时更新
label.after(1000, update_label) # 设置更新的间隔时间(以毫秒为单位,这里是1秒)
# 创建窗口
window = tk.Tk()
# 创建标签
label = tk.Label(window, text="Count: 0")
label.pack()
# 定时更新标签内容
counta = 0
update_label()
# 启动窗口的事件循环
window.mainloop()
#函数三
# import tkinter as tk
# def long_running_task():
# # 模拟一个耗时操作
# import time
# time.sleep(5)
# print("Long running task finished!")
# def button_click():
# print("Button clicked!")
# window.after(0, long_running_task)
# window = tk.Tk()
# button = tk.Button(window, text="Click Me", command=button_click)
# button.pack()
# window.mainloop()
#函数四
# import tkinter as tk
# master = tk.Tk()
# def my_mainloop():
# print ("Hello World!")
# master.after(1000, my_mainloop)
# master.after(1000, my_mainloop)
# master.mainloop()