2017-07-25 17 views
0

私はPython GUIを持っています.Tkinterを作成しています。これは初めてのGUI/Tkinterで作業していますので、私には負担してください!Python Tkinter保存ポイント座標

ポイントの場所/座標を変数に保存しようとしています。私がこれまでに持っていたコードでは、BMPイメージをアップロードし、イメージ上のある点をクリックするとその点がマークされます。最初の3点の座標を変数に代入する方法を解明しようとしていますが、これらの点のうち3点すべてを交差する最適な楕円を作成するために使用します。これをどうやってやりますか?位置を保存するには

from tkinter import * 
from PIL import Image, ImageTk 

class Window(Frame): 
    # Define settings upon initialization. 
    def __init__(self, master=None): 
     # parameters that you want to send through the Frame class. 
     Frame.__init__(self, master) 

     # reference to the master widget, which is the tk window 
     self.master = master 

     # with that, we want to then run init_window, which doesn't yet 
     exist 
     self.init_window() 

    # Creation of init_window 
    def init_window(self): 
     # changing the title of our master widget 
     self.master.title("GUI") 

     # allowing the widget to take the full space of the root window 
     self.pack(fill=BOTH, expand=1) 

     # creating a menu instance 
     menu = Menu(self.master) 
     self.master.config(menu=menu) 

     # create the file object) 
     file = Menu(menu) 

     # adds a command to the menu option, calling it exit, and the 
     # command it runs on event is client_exit 
     file.add_command(label="Exit", command=self.client_exit) 

     # added "file" to our menu 
     menu.add_cascade(label="File", menu=file) 

     # create the file object) 
     analyze = Menu(menu) 

     # adds a command to the menu option, calling it exit, and the 
     # command it runs on event is client_exit 
     analyze.add_command(label="Region of Interest", 
     command=self.regionOfInterest) 

     # added "file" to our menu 
     menu.add_cascade(label="Analyze", menu=analyze) 
     load = Image.open("ap41.ddr.brf.sdat.bmp") 
     render = ImageTk.PhotoImage(load) 

     # labels can be text or images 
     img = Label(self, image=render) 
     img.image = render 
     img.place(x=0, y=0) 

    def regionOfInterest(self): 
     root.config(cursor="plus") 
     canvas.bind("<Button-1>", self.imgClick) 


    def client_exit(self): 
     exit() 

    def imgClick(self, e): 
     x = canvas.canvasx(e.x) 
     y = canvas.canvasy(e.y) 
     pos.append((x, y)) 
     canvas.create_line(x - 5, y, x + 5, y, fill="red", tags="crosshair") 
     canvas.create_line(x, y - 5, x, y + 5, fill="red", tags="crosshair") 


# root window created. Here, that would be the only window, but 
# you can later have windows within windows. 
root = Tk() 

# loads exact size of image 
imgSize = Image.open("ap41.ddr.brf.sdat.bmp") 
tkimage = ImageTk.PhotoImage(imgSize) 
w, h = imgSize.size 

# creates canvas 
canvas = Canvas(root, width=w, height=h) 
canvas.create_image((w/2,h/2),image=tkimage) 
canvas.pack() 

# loads exact dimentsion from img size 
geometry = "%dx%d" % (w,h) 
root.geometry(geometry) 

# creation of an instance 
app = Window(root) 

# mainloop 
root.mainloop() 

答えて

1

は、リストを使用することができます:

は、ここに私のコードです。タプルはクラス属性に格納できます。

imgClick関数をクラスに移動して、クラス属性を簡単に利用できるようにします。

次に、init_windowメソッドをここでは冗長として削除します。

また、カウンタを追加すると、3回のクリックに達すると、プログラムはマップをマークしなくなり、ボタンのバインドを削除します。

新しいクラスの属性:

def imgClick(self, event): 

    if self.counter < 3: 
     x = canvas.canvasx(event.x) 
     y = canvas.canvasy(event.y) 
     self.pos.append((x, y)) 
     print(self.pos) 
     canvas.create_line(x - 5, y, x + 5, y, fill="red", tags="crosshair") 
     canvas.create_line(x, y - 5, x, y + 5, fill="red", tags="crosshair") 
     self.counter += 1 
    else: 
     canvas.unbind("<Button 1>") 
     root.config(cursor="arrow") 
     self.counter = 0 
self.posの保存された値をプリントアウトしますprint文がありますがわかります

self.pos = [] self.counter = 0

はその後、我々はimgClick方法を変更しますクリックの分析中にクリックごとに

は、このコードを見て、私はあなたが何を聞かせ:

from tkinter import * 
from PIL import Image, ImageTk 

class Window(Frame): 

    def __init__(self, master=None): 
     Frame.__init__(self, master) 

     self.master = master 
     self.pos = [] 
     self.master.title("GUI") 
     self.pack(fill=BOTH, expand=1) 

     self.counter = 0 

     menu = Menu(self.master) 
     self.master.config(menu=menu) 

     file = Menu(menu) 
     file.add_command(label="Exit", command=self.client_exit) 
     menu.add_cascade(label="File", menu=file) 
     analyze = Menu(menu) 

     analyze.add_command(label="Region of Interest", 
     command=self.regionOfInterest) 

     menu.add_cascade(label="Analyze", menu=analyze) 
     load = Image.open("ap41.ddr.brf.sdat.bmp") 
     render = ImageTk.PhotoImage(load) 

     img = Label(self, image=render) 
     img.image = render 
     img.place(x=0, y=0) 

    def regionOfInterest(self): 
     root.config(cursor="plus") 
     canvas.bind("<Button-1>", self.imgClick) 


    def client_exit(self): 
     exit() 

    def imgClick(self, event): 

     if self.counter < 3: 
      x = canvas.canvasx(event.x) 
      y = canvas.canvasy(event.y) 
      self.pos.append((x, y)) 
      print(self.pos) 
      canvas.create_line(x - 5, y, x + 5, y, fill="red", tags="crosshair") 
      canvas.create_line(x, y - 5, x, y + 5, fill="red", tags="crosshair") 
      self.counter += 1 
     else: 
      canvas.unbind("<Button 1>") 
      root.config(cursor="arrow") 
      self.counter = 0 


root = Tk() 
imgSize = Image.open("ap41.ddr.brf.sdat.bmp") 
tkimage = ImageTk.PhotoImage(imgSize) 
w, h = imgSize.size 

canvas = Canvas(root, width=w, height=h) 
canvas.create_image((w/2,h/2),image=tkimage) 
canvas.pack() 

root.geometry("%dx%d"%(w,h)) 
app = Window(root) 
root.mainloop() 
+0

あなたは素晴らしい作品を提供したコードを!また、各ステップのステップのプロセスとあなたの理論的根拠にも感謝しています。それはかなり役に立ちました – FLCL

+0

@FLCL:あなたは歓迎です:)喜んで助けてください。 –

+0

ねえ!だから私は代わりに4点をしようとしています。私はself.counter <4に変更しようとしましたが、それでも私は3点をプロットすることができます。私は何か見落としていますか? – FLCL

関連する問題