简体   繁体   English

Tkinter python代码中的无限循环

[英]Infinite loop in Tkinter python code

I'm trying to run a simple Tkinter program that opens a program when you click a button. 我正在尝试运行一个简单的Tkinter程序,当您单击按钮时会打开一个程序。 The code is listed below. 该代码在下面列出。 I use a command to call a program that then calls a fortran program. 我使用命令来调用程序,然后再调用fortran程序。 However, when I click on the button, it opens the program but the menu of the program i'm calling goes into an infinite loop......the offending code seems to be in the button1Click module. 但是,当我单击按钮时,它会打开程序,但是我正在调用的程序菜单进入无限循环……令人讨厌的代码似乎在button1Click模块中。

Any help is greatly appreciated. 任何帮助是极大的赞赏。

Thanks 谢谢

from Tkinter import * 
import os, sys 
from win32com.client import Dispatch 
xlApp=Dispatch('Excel.Application') 
_PSSBINPATH=r"C:\Program Files\PTI\PSSE32\PSSBIN" 
os.environ['PATH']=_PSSBINPATH+';'+os.environ['PATH'] 
sys.path.insert(0,_PSSBINPATH) 
import redirect; redirect.psse2py() 
import psspy 

class MyApp: 
    def __init__(self, parent): 
        self.myParent = parent  ### (7) remember my parent, the root 
        self.myContainer1 = Frame(parent) 
        self.myContainer1.pack() 

        self.button1 = Button(self.myContainer1) 
        self.button1.configure(text="OK", background= "green") 
        self.button1.pack(side=LEFT) 
        self.button1.bind("<Button-1>", self.button1Click) ### (1) 

        self.button2 = Button(self.myContainer1) 
        self.button2.configure(text="Cancel", background="red") 
        self.button2.pack(side=RIGHT) 
        self.button2.bind("<Button-1>", self.button2Click) ### (2) 

    def button1Click(self,event):    ### (3) 
        psspy.runiplanfile(r"C:\MNTACT\Contingency Program\work\contingency-31-4.irf") 
        if self.button1["background"] == "green": ### (4) 
            self.button1["background"] = "yellow" 
        else: 
            self.button1["background"] = "green" 

    def button2Click(self, event):  ### (5) 
        self.myParent.destroy()     ### (6) 


root = Tk() 
myapp = MyApp(root) 
root.mainloop() 

What makes you think there's an infinite loop happening? 是什么让您认为正在发生无限循环? I see no loop in button1Click, unless the loop is in runiplanfile . 我看不到button1Click中没有循环,除非该循环在runiplanfile Are you using "infinite loop" to mean simply that the GUI has stopped responding? 您是否使用“无限循环”来简单地表示GUI已停止响应?

Tkinter is single threaded and cannot process events except via the event loop. Tkinter是单线程的,除非通过事件循环,否则无法处理事件。 If one event takes a long time to process, the GUI will hang until the processing of that event is completed. 如果处理一个事件需要很长时间,则GUI将挂起,直到该事件的处理完成。 If you're exec'ing an external process and waiting for it to complete, your GUI will appear to be frozen until that process finishes. 如果您正在执行一个外部进程并等待其完成,则该GUI似乎一直处于冻结状态,直到该进程完成为止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM