2016-11-15 5 views
2

私はTkinterのを使用してPythonアプリケーションに取り組んでいます。私がやりたいことは、キャンバスの座標を描画することです。ポイントはリストに記録され、後で計算を行うことができます。 は、それが不可能な場合は、この操作を行うことができ、他のツールやGUIプラットフォームを勧めますか?python tkinter canvasをマウスで描画し、ポイントをリストに取得しますか?

編集:私は今のところ持っているどのようなリストからポイントを取り、キャンバスに描くことができるアプリケーションです。私は逆の方法で作業したいと思います。あなたが望むよう

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() 

-Thanks

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

+1

これまでに何を試みましたか?今、この質問はちょうど "私のためのコードを書く"です。 – Lafexlos

+0

このコードは実行されません。 'はAttributeError:「波形」オブジェクトが属性のCustomizedWaveformes'' –

+0

を持っていないあなたはときにマウスが移動するためにバインディングを追加することができ、そしてあなたは、マウスをクリックしたとき。バインドされた関数に与えられた情報を使用して線を描くことができます。 –

答えて

2

スタートは次に詳しく説明します。キャンバスにバインド運動、キャンバス上の点を描き、リストにペアを追加し、バインドは、ポイントのストリームをフィルタリングし、代わりに、またはそれに加えてクリックするなど

+0

非常に良い答え。これは簡単だとは信じられません。どうもありがとう!!! –

2

は非常に便利なヒントを提供するためにあなたにテリーをありがとうございます。私はそれを働かせることができた。冗長点が記録されているため、これは最適な解決策ではないことに注意してください。だから私はそれをたくさん出さなければならないのです。しかし、少なくともそれは動作します。

import tkinter as tk 

class ExampleApp(tk.Tk): 
    def __init__(self): 
     tk.Tk.__init__(self) 
     self.previous_x = self.previous_y = 0 
     self.x = self.y = 0 
     self.points_recorded = [] 
     self.canvas = tk.Canvas(self, width=400, height=400, bg = "black", cursor="cross") 
     self.canvas.pack(side="top", fill="both", expand=True) 
     self.button_print = tk.Button(self, text = "Display points", command = self.print_points) 
     self.button_print.pack(side="top", fill="both", expand=True) 
     self.button_clear = tk.Button(self, text = "Clear", command = self.clear_all) 
     self.button_clear.pack(side="top", fill="both", expand=True) 
     self.canvas.bind("<Motion>", self.tell_me_where_you_are) 
     self.canvas.bind("<B1-Motion>", self.draw_from_where_you_are) 

    def clear_all(self): 
     self.canvas.delete("all") 

    def print_points(self): 
     if self.points_recorded: 
      self.points_recorded.pop() 
      self.points_recorded.pop() 
     self.canvas.create_line(self.points_recorded, fill = "yellow") 
     self.points_recorded[:] = [] 

    def tell_me_where_you_are(self, event): 
     self.previous_x = event.x 
     self.previous_y = event.y 

    def draw_from_where_you_are(self, event): 
     if self.points_recorded: 
      self.points_recorded.pop() 
      self.points_recorded.pop() 

     self.x = event.x 
     self.y = event.y 
     self.canvas.create_line(self.previous_x, self.previous_y, 
           self.x, self.y,fill="yellow") 
     self.points_recorded.append(self.previous_x) 
     self.points_recorded.append(self.previous_y) 
     self.points_recorded.append(self.x)  
     self.points_recorded.append(self.x)   
     self.previous_x = self.x 
     self.previous_y = self.y 

if __name__ == "__main__": 
    app = ExampleApp() 
    app.mainloop() 
+0

イメージにエクスポートするにはどうすればいいですか? btw、これは他の答え、おかげでより良いです.... –

+1

私はこれまでにそれをやったことがない。だから、私は別のSOのポストでこれを見つけました。それが役に立てば幸い!!! https://stackoverflow.com/questions/9886274/how-can-i-convert-canvas-content-to-an-image –