python - 使用实时数据创建的线条放大 tkinter 画布
问题描述
我是 Tkinter 的新手,并试图弄清楚如何能够在画布上放大和缩小,而不会弄乱由实时数据创建的画线。每秒都会绘制一条新线,并且应该连接到前一条线,但是如果我放大画布,则将绘制的下一条线不会连接到前一条线。似乎所有的线条都是基于一些窗口坐标绘制的(无论我放大或缩小多少都是一致的),而不是基于画布本身。我希望能够根据需要放大和缩小,并且仍然看到从画布的左上角到画布的右下角绘制的一条线。我试图提供一些代码以使其更易于理解。
import tkinter as tk
import threading
import time
root = tk.Tk()
pressed = False
flag_run = False
GRID_W = 500
GRID_H = 500
def thread_entry(name):
print("<starting thread>")
i = 0
j = 0
while flag_run:
time.sleep(1)
canvas.create_line(i, j, i+1, j+1, fill="red")
i += 1
j += 1
print("<ending thread>")
def start():
global flag_run
flag_run = True
global thread
thread = threading.Thread(target=thread_entry, args=(1,))
thread.start()
def stop():
global flag_run
if flag_run is True:
flag_run = False
global thread
thread.join(timeout=0.1)
# move
def move_start(event):
canvas.scan_mark(event.x, event.y)
def move_move(event):
canvas.scan_dragto(event.x, event.y, gain=1)
# move
def pressed2(event):
global pressed
pressed = not pressed
canvas.scan_mark(event.x, event.y)
def move_move2(event):
if pressed:
canvas.scan_dragto(event.x, event.y, gain=1)
# windows zoom
def zoomer(event):
if (event.delta > 0):
canvas.scale("all", event.x, event.y, 1.1, 1.1)
elif (event.delta < 0):
canvas.scale("all", event.x, event.y, 0.9, 0.9)
canvas.configure(scrollregion = canvas.bbox("all"))
def zooming_in(event):
canvas.scale("all", event.x, event.y, 1.1, 1.1)
def zooming_out(event):
canvas.scale("all", event.x, event.y, 0.9, 0.9)
canvas = tk.Canvas(root, width=GRID_W, height=GRID_H, background="white")
canvas.grid(row=0, column=0, sticky="nsew")
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
for i in range(GRID_W//10 + 1):
canvas.create_line(i*10, 0, i*10, GRID_H, fill="#c9c9c9")
for j in range(GRID_H//10 + 1):
canvas.create_line(0, j*10, GRID_W, j*10, fill="#c9c9c9")
canvas.create_line(GRID_W // 2, 0, GRID_W // 2, GRID_H, fill="black", width=2)
canvas.create_line(0, GRID_H // 2, GRID_W, GRID_H // 2, fill="black", width=2)
canvas.bind("<ButtonPress-1>", move_start)
canvas.bind("<B1-Motion>", move_move)
canvas.bind("<ButtonPress-2>", pressed2)
canvas.bind("<Motion>", move_move2)
zoom_in = tk.Button(root, text="+")
zoom_in.grid(row=1, column=0)
zoom_in.bind("<Button-1>", zooming_in)
zoom_out = tk.Button(root, text="-")
zoom_out.grid(row=1, column=1)
zoom_out.bind("<Button-1>", zooming_out)
button = tk.Button(root, text="Start", command = start)
button.grid(row=2, column=0)
button_s = tk.Button(root, text="Stopp", command = stop)
button_s.grid(row=2, column=1)
root.mainloop()
我感谢我能得到的所有帮助和指导!
解决方案
try stopping the flag_run while you hit the start button (zooming_in(event)) and continue when done zooming. try remembering where the line was, and keep logging the data while you zoom in and out so you are not missing any values as you currently do.
推荐阅读
- python - 在 Python 中的 MIP 求解器或工具的约束中使用 If else 语句
- python - 在编写用于在一个列表中转换两个列表的代码时,我在 python 中遇到此错误“并非所有参数都在字符串格式化期间转换”
- python - 用于 ETL 的管道 python 脚本
- dart - Dart 语言中的 ^ 运算符是什么?
- camera - Raspberry Pi 上的运动:USB 4 通道复合视频捕获设备上的 2 个摄像头出现问题
- typescript - 将范围映射到值的最佳数据结构是什么?
- java - Android 11 上 createDeleteRequest() 的 OnActivtyResult
- haskell - Haskell 类型推断与 GADT 和类型变量的类型类约束
- ios - 待发布后上传具有相同版本的构建
- c# - C# 返回主窗体