使用鼠标在python tkinter画布上绘制并获取指向列表的点? [英] Draw on python tkinter canvas using mouse and obtain points to a list?

查看:32
本文介绍了使用鼠标在python tkinter画布上绘制并获取指向列表的点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 tkinter 开发 Python 应用程序.我想要做的是在画布坐标上绘制,点将记录到列表中,以便我稍后进行计算.如果不可能,您会推荐其他可以做到这一点的工具或 GUI 平台吗?

I'm working on a Python application using tkinter. What I want to do is to draw on canvas coordinates, and the points will be recorded to a list so I can do calculation later. If it's not possible, would you recommend any other tools or GUI platform that can do this?

到目前为止,我有一个可以从列表中获取点并在画布上绘制的应用程序.我也想要相反的工作方式.

What I have so far is an application that can take points from a list and draw on the canvas. I would like the opposite way to work too.

from tkinter import *
from tkinter.filedialog import askopenfilename
from tkinter.messagebox import showerror
from tkinter import ttk
import tkinter
import threading

another_points = []

class Waveform(Frame):
   def __init__(self, master):
      ttk.Frame.__init__(self, master)
      self.master = master

      self.points = [10, 20, 30]
      self.create_widget()

   def create_widget(self):

      self.LoadFrame = ttk.LabelFrame(self.master, text = "Load")
      self.LoadFrame.pack(side = "left", padx = 10, pady = 10)
      self.PlotFrame()

      self.anotherFrame = ttk.LabelFrame(self.LoadFrame, text = "Browse")
      self.anotherFrame.pack(fill = "both", padx = 10, pady = 10)
      self.OpenFileButton = ttk.Button(self.anotherFrame, text = "Browse", command = self.load_file)
      self.OpenFileButton.grid(row = 0, column = 0)

   def load_file(self):
      fname = askopenfilename(filetypes = (("Text files", "*.txt"), ("All files", "*.*")))
      another_points[:] = []
      self.points[:] = []
      if fname:
         try:
            print (fname)
            with open(fname) as f:
               for line in f:
                  another_points.append(float(line.rstrip()))

            self.points = another_points

         except:
            print ("Error")
         return


   def PlotFrame(self):
      self.PlotFrame = ttk.LabelFrame(self.master, text = "X/Y Coordinates")
      self.PlotFrame.pack(side = LEFT, padx = 10, pady = 10)

      self.width = 400
      self.height = 350
      self.pressure = 75
      self.x_increment = 1
      self.x_factor = 0.04
      self.y_amplitude = 80


      def draw():
         self.GraphFrame.delete(self.sin_line)
         self.xy1 = []
         self.xy2 = self.points
         for x in range(len(self.xy2)):
            self.xy1.append(x + 25)
            self.xy1.append(self.height - self.xy2[x])

         self.sin_line = self.GraphFrame.create_line(self.xy1, fill = "yellow")

      self.UpdateButton = ttk.Button(self.PlotFrame, text = "Update", command = draw)
      self.UpdateButton.pack()

      self.GraphFrame = Canvas(self.PlotFrame, width = self.width, height = self.height, bg = "black")

      self.GraphFrame.pack()
      self.original = []

      self.mock = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
      for x in range(len(self.mock)):
         self.original.append((x + 5)*10)
         self.original.append((self.height - self.mock[x])/100)

      self.sin_line = self.GraphFrame.create_line(self.original, fill = "yellow")


if __name__ == "__main__":
    master = Tk()
    Waveform(master).pack(side = "top", fill ="both", expand = True)
    master.mainloop()

-谢谢

推荐答案

开始

import tkinter as tk
root = tk.Tk()
def mmove(event):
    print(event.x, event.y)
root.bind('<Motion>', mmove)
root.mainloop()

然后根据需要详细说明.将运动绑定到画布,在画布上绘制点,将对附加到列表中,替代或另外绑定点击,过滤点流等.

Then elaborate as you wish. Bind motion to a canvas, draw points on the canvas, append pairs to a list, bind clicks instead or in addition, filter the stream of points, etc.

这篇关于使用鼠标在python tkinter画布上绘制并获取指向列表的点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆