tkinter 多线程计时器、Label、Button
import tkinter as tk
from threading import Thread, Event
hour = 0
class ControlThread(Thread):
def __init__(self):
self._stop_event = Event()
Thread.__init__(self)
def run(self):
f1()
def terminate(self):
self._stop_event.set()
def stopped(self):
return self._stop_event.is_set()
def f1():
def count():
global hour
global x
hour += 1
hour %= 24
label_hour.config(text=str(hour))
x = label_hour.after(3600, count)
count()
def run():
global t1
t1 = ControlThread()
t1.start()
def stop():
global t1
label_hour.after_cancel(x)
t1.terminate()
root = tk.Tk()
root.title("time")
label_time = tk.Label(root, text="时刻(h)")
label_time.grid(row=0, column=0, columnspan=3)
label_hour = tk.Label(root, bg="lightblue", height=2, width=10)
label_hour.grid(row=0, column=0, columnspan=3)
button_run = tk.Button(root, text="Run", command=run)
button_run.grid(row=1, column=0)
button_stop = tk.Button(root, text="Stop", command=stop)
button_stop.grid(row=1, column=2)
root.mainloop()
tkinter Text、Button、messagebox
import tkinter as tk
from tkinter import messagebox
def submit():
'''get(1.0, tk.END)是从第一个字符读到末尾,假设输入为A-B'''
inputs = text_input.get(1.0, tk.END).replace('\n', '').split('-')
result = []
for i in result:
result.append(i)
print(result)
messagebox.showinfo("Info", "Submit Successfully.")
text_input.delete(1.0, tk.END)
root = tk.Tk()
root.title("input&submit")
text_input = tk.Text(root, bg="yellow", height=2, width=10)
text_input.grid(row=0, column=0, columnspan=3)
button_ok = tk.Button(root, text="OK", command=submit)
button_ok.grid(row=1, column=1)
root.mainloop()
tkinter Canvas
import tkinter as tk
x = {
'A': 120, 'F': 360, 'I': 680, 'J': 760,
'B': 40, 'E': 200, 'H': 600, 'K': 840,
'C': 280, 'G': 520, 'D': 440, 'L': 920
}
y = {
'A': 440, 'F': 200, 'I': 360, 'J': 40,
'B': 280, 'E': 120, 'H': 280, 'K': 200,
'C': 360, 'G': 120, 'D': 440, 'L': 360
}
root = tk.Tk()
root.title("canvas")
canvas_map = tk.Canvas(root, bg="lightgray", height=480, width=960)
canvas_map.grid(row=0, column=1, rowspan=20)
'''x轴,虚线'''
canvas_map.create_line(0, 240, 960, 240, dash=(4, 4))
'''y轴,虚线'''
canvas_map.create_line(480, 0, 480, 480, dash=(4, 4))
'''绘制不同位置的四个点'''
canvas_map.create_oval(x['C']-10, y['C']-10, x['C']+10, y['C']+10, fill="lightgreen")
canvas_map.create_text(x['C'], y['C'], text="C", font=10)
canvas_map.create_oval(x['E']-10, y['E']-10, x['E']+10, y['E']+10, fill="lightgreen")
canvas_map.create_text(x['E'], y['E'], text="E", font=10)
canvas_map.create_oval(x['F']-10, y['F']-10, x['F']+10, y['F']+10, fill="lightgreen")
canvas_map.create_text(x['F'], y['F'], text="F", font=10)
canvas_map.create_oval(x['J']-10, y['J']-10, x['J']+10, y['J']+10, fill="lightgreen")
canvas_map.create_text(x['J'], y['J'], text="J", font=10)
'''绘制不同颜色的实线连线'''
canvas_map.create_line(x['C'], y['C'], x['E'], y['E'], fill="yellow")
canvas_map.create_line(x['E'], y['E'], x['F'], y['F'], fill="blue")
canvas_map.create_line(x['F'], y['F'], x['J'], y['J'], fill="red")
root.mainloop()
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/90a4a920a7ce36c2a934fa68041232cf.gif#pic_center)