Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 单击按钮后,mainloop将重新执行4次_Python_Tkinter - Fatal编程技术网

Python 单击按钮后,mainloop将重新执行4次

Python 单击按钮后,mainloop将重新执行4次,python,tkinter,Python,Tkinter,我是新来的。我有一个程序,在点击一个按钮后,它从另一个模块(anotherModule)调用一个函数(processTask),这个函数需要几分钟才能运行 问题是,点击按钮后,几秒钟后,它会弹出4个主程序窗口。我真的不明白为什么会这样 我的代码中是否有什么东西使程序的行为如此 提前谢谢 from Tkinter import * import tkMessageBox from anotherModule import * import ttk from PIL import ImageTk,

我是新来的。我有一个程序,在点击一个按钮后,它从另一个模块(anotherModule)调用一个函数(processTask),这个函数需要几分钟才能运行

问题是,点击按钮后,几秒钟后,它会弹出4个主程序窗口。我真的不明白为什么会这样

我的代码中是否有什么东西使程序的行为如此

提前谢谢

from Tkinter import *
import tkMessageBox
from anotherModule import *
import ttk
from PIL import ImageTk, Image
import threading

app = Tk()
app.title("Title")
app.geometry('450x300+200+200')

img = ImageTk.PhotoImage(Image.open("logo.png"))
panel = Label(app, image = img)
panel.pack(side = "bottom", fill = "both", expand = "yes")

message = ''


def handle_click():
    Instruction.config(text='Rquest is being processed')
    pb.start()
    def callback():
        processTask()
        pb.stop()
        pb.config(value=100)
        Instruction.config(text='Fin')


    t = threading.Thread(target=callback)
    t.start()

B = Button(app, command = handle_click)
img2 = ImageTk.PhotoImage(Image.open("GenHor.PNG"))
B.config(image = img2)
B.pack()
Instruction = Label(app, text=message, font='size, 14')
Instruction.pack()

pb = ttk.Progressbar(app,orient='horizontal',length=300,mode='determinate')

pb.pack()

app.mainloop() 
编辑

我知道这不是最好的解决方案,但是,有没有一种方法可以计算执行情况并执行以下操作:

if counter == 1:
    app.mainloop()

因此,只有在第一次执行时才会运行mainloop?

不确定这是否是导致问题的原因,但tkinter不是线程安全的。避免在线程中修改GUI。很可能其他模块正在创建小部件。发布的代码不可能创建多个窗口。这很有趣。事实并非如此。另一个模块只是处理数据