2016-04-19 118 views
0

嗨,我是一名學生,我第一次使用ktinker。我目前正在創建符合此順序的程序:stop tkinter mainloop擾亂matplotlib

  1. 在matplotlib打開地圖
  2. 一個彈出窗口要求用戶在信息
  3. 輸入時,在地圖網格用戶點擊座標和用戶 信息存儲在一個字典
  4. matplotlib生成的位置

麻煩的是,當我打電話彈出的窗口中我已經寫了我一個情節點n tkinter,調用mainloop()函數打破matplotlib窗口,並且不會在地圖窗口關閉之前生成繪圖點。

擺脫主循環修復它,但然後意味着我不能從彈出的數據。 任何人都知道我怎麼能阻止一個干擾另一個?我必須將它們放在單獨的類中,因爲Im意在在我的代碼中演示模塊化。 這裏是主代碼

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

,這裏是從另一個文件

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 

回答

0

稱爲彈出代碼如果我underdstood你的問題吧,然後嘗試添加自己的循環。

  1. 拆下pop.mainloop()

  2. 使代碼是用於清潔器代碼的類


    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 
  • 創建自己的在的末端循環初始化
  • 
         def __init__(self): 
          #... 
          while self.data_sent == False: 
           root.update() 
    
        while data_sent == False: 
         pop.update() 
    
    
  • 通過

    mypopup = userinput()

  • 好運打電話給你的彈出;)