2016-04-19 13 views
0

hi Ich bin ein Student und ich arbeite zum ersten Mal mit ktinker. Ich versuche zur Zeit ein Programm zu erstellen, die diese Reihenfolge folgt: wirdstop tkinter mainloop stört matplotlib

  1. öffnen Sie eine Karte in matplotlib
  2. ein Popup-Fenster für Benutzer
  3. in Informationen einzugeben aufgerufen, wenn der Benutzer klickt auf der Karte Raster der in einem Wörterbuch Koordinaten und Benutzer info ist

Das Problem Ort gespeichert

  • matplotlib erzeugt einen Plot Punkt auf dem, wenn ich das Popup-Fenster aufrufen habe ich geschrieben i n tkinter, ruft die Funktion mainloop() auf, bricht das Fenster matplotlib und generiert die Plotpunkte erst, wenn das Kartenfenster geschlossen ist.

    loswerden der Hauptschleife behebt es aber dann bedeutet, ich kann nicht die Daten aus dem Popup erhalten. Wer weiß, wie ich verhindern kann, dass jemand sich in den anderen einmischt? Ich muss sie in getrennten Klassen halten, da ich Modularität in meinem Code demonstrieren möchte. hier ist der Hauptcode

    import matplotlib.pyplot as plt 
    import matplotlib.image as mpimg 
    import numpy as np 
    import popup1 as ap 
    
    __author__ = "k1221169" 
    __date__ = "$15-Nov-2015 11:29:21$" 
    
    
    markerList = [] 
    xplots = [] 
    yplots = [] 
    desc = '' 
    title = '' 
    fig = plt.figure() 
    ax = fig.add_subplot(111) 
    ax.plot(np.random.rand(10)) 
    img = mpimg.imread('overland map 1.png') 
    imgplot = plt.imshow(img) 
    plt.ylim(ymax=1) 
    plt.xlim(xmin=1) 
    
    
    def mrkApnd(a,b): 
        a.update(b) 
        print a 
        markerList.append(a) 
    
    def onclick(event): 
    
        if spacecheck(event.xdata, event.ydata): 
         #print markerList[0]['x'] 
         idx = np.abs(xplots - event.xdata).argmin() 
         print xplots[idx] 
    
         for i in markerList: 
          if xplots[idx] == i['x'] : 
           print i 
           #c = i 
           ap.useroutput(i) 
        else: 
         input1 = ap.userinput() 
         input2 = {'x':event.xdata, 'y':event.ydata} 
         input1['title'] = title 
         input1['desc'] = desc 
         mrkApnd(input1,input2) 
    
         drawMarks() 
         print input1 
    
        return markerList 
    cid = fig.canvas.mpl_connect('button_press_event', onclick) 
    
    def drawMarks(): 
        plt.ion() 
        for i in markerList: 
         xplots.append(i['x']) 
         yplots.append(i['y']) 
         plt.plot(i['x'], i['y'], i['type']) 
    
    
    
    
    
    def spacecheck(x,y): 
        a = bool 
        if np.isclose(xplots, x, atol=50.0).any() and np.isclose(yplots, y, atol=50.00).any(): 
         a=True 
         print 'yes' 
         return a 
    plt.draw() 
    plt.show() 
    

    und hier ist der Popup-Code aus einer anderen Datei namens

    from Tkinter import * 
    
    class popup1(): 
        def __init__(self): 
    
         pass 
    
    def userinput(): 
    
    
        pop = Toplevel() 
    
    
        pop.title("marker") 
        pop.geometry("300x500+200+200") 
    
        #string for title 
    
        frame = Frame(pop) 
        entry = Entry(frame) 
        entry.pack(side = TOP) 
        frame.pack(padx =20, pady =20) 
    
    
    
        #radius button for visibility 
        frame2 = Frame(pop) 
        selection = StringVar() 
        radio_1 = Radiobutton(frame2, text = 'Character', variable = selection, value = 'ob') 
        radio_2 = Radiobutton(frame2, text = 'Item', variable = selection, value = 'or') 
        radio_3 = Radiobutton(frame2, text='Event', variable = selection, value = 'oy') 
    
        radio_1.select() 
        radio_1.pack(side = LEFT) 
        radio_2.pack(side = LEFT) 
        radio_3.pack(side = LEFT) 
        frame2.pack(padx =30, pady =30) 
        #radius button for marker type 
        frame3 = Frame(pop) 
        visible = bool 
        check_1 = Checkbutton(frame3, text = 'GM Only', variable = visible, onvalue= True, offvalue= False) 
    
        check_1.pack(side = LEFT) 
        frame3.pack(padx =30, pady =30) 
        #string for user input 
        frame4 = Frame(pop) 
        entry4 = Entry(frame4) 
        entry4.pack(side = LEFT) 
        frame4.pack(padx =20, pady =20) 
    
    
    
        def infoPass(): 
         #info1 = {'title': entry.get(), 'type': selection.get(), 'vis': visible, 'Desc': entry4.get()} 
         #info.update(info1) 
         #print info 
    
         pop.destroy() 
    
        #buttons 
        label = Label(pop, text="", height=0, width=100) 
        label.pack() 
        b = Button(pop, text="Cancel", width=20, command= pop.destroy) 
        b.pack(side='bottom',padx=5,pady=5) 
        b2 = Button(pop, text="Save", width=20, command= infoPass ) 
        b2.pack(side='bottom',padx=5,pady=5) 
        info = {'title': entry.get(), 'type': selection.get(), 'vis': visible, 'desc': entry4.get()} 
        pop.mainloop() 
        return info 
    
  • Antwort

    0

    Wenn ich Ihre Frage richtig underdstood, dann versuchen Sie Ihre eigene Schleife hinzuzufügen.

    1. Remove pop.mainloop()

    2. Stellen der Code eine Klasse für einen saubereren Code

    
        class userinput: 
         def __init__(self): 
          #open your window here and style it etc. and: 
          self.data_sent = False 
         def infopass(self): 
          self.data_sent = True 
          #your infopass code here and 
    
    1. Selbst designen Schleife am Ende von init:
    
         def __init__(self): 
          #... 
          while self.data_sent == False: 
           root.update() 
    
        while data_sent == False: 
         pop.update() 
    
    
    1. Ihr Popup Call by

      mypopup = userinput()

    Viel Glück;)