一次单击就生成多个tkinter窗口

时间:2019-06-25 10:36:01

标签: tkinter python-3.7

当我单击画布上的一个点(在plot [0] == 2,模式下)而不是一个点时,将打开多个(大约10个)tkinter窗口,询问经度和纬度

def on_press(self, event):
    initial_x=round(event.xdata,2)
    initial_y=round(event.ydata,2)
    if(plot[0]=='2'):
        master = tk.Tk()
        tk.Label(master, text="Source ").grid(row=0)
        tk.Label(master, text="Destination ").grid(row=1)                
        e = tk.Entry(master)
        f = tk.Entry(master)            
        e.grid(row=0, column=1)
        f.grid(row=1, column=1)

        def show_entry_fields():
            src = e.get()
            dest = f.get()    
            global flag
            flag = 0
            for i in range(0, nRoute):
                if((c1[i]==src and c2[i]==dest) or (c1[i]==dest and c2[i]==src)):
                    c9[i]='0';
                    flag = 1
            if(flag==0):
                # print("no two such locations found")
                initialPaths = extraPaths
                for i in range(0, initialPaths):
                    if((kneeSrc[i]==src and kneeDest[i]==dest) or (kneeSrc[i]==dest and kneeDest[i]==src)):
                        pass

            master.destroy()

        tk.Button(master, text='Quit', command=master.destroy).grid(row=3, column=0, sticky=tk.W)
        tk.Button(master, text='Okay', command=show_entry_fields).grid(row=3, column=1, sticky=tk.W, pady=4)

        self.after(100,master.mainloop())
        window.destroy()
    if(plot[0]=='1'):
        for i in range(0,nBus):
            if((round(busDict[col2[i]][2],2)==initial_x) and (round(busDict[col2[i]][3],2)==initial_y)):
                busDict[col2[i]][2]=0.0 
                busDict[col2[i]][3]=0.0   
    if event.inaxes != self.point.axes: return
    if DraggablePoint.lock is not None: return
    contains, attrd = self.point.contains(event)
    if not contains: return
    self.press = (self.point.center), event.xdata, event.ydata
    DraggablePoint.lock = self

    # draw everything but the selected rectangle and store the pixel buffer
    canvas = self.point.figure.canvas
    axes = self.point.axes
    self.point.set_animated(True)
    canvas.draw()

如何更改代码,以使单击仅生成一个tkinter窗口

1 个答案:

答案 0 :(得分:0)

有一个简单的解决方法。初始化新变量self.onpress_open = False 比添加self.onpress_open = True并将条件为self.onpress_open == False的if语句放在您创建新窗口的部分周围。 然后,不要忘记将self.onpress_open = False放在关闭窗口的任何地方。

那只是修饰。它确实解决了无意中多次运行函数的问题。