2013-03-08 133 views
0

我试图运行一个简单的Tkinter程序,当您单击一个按钮时将打开一个程序。代码如下所示。我使用一个命令来调用一个程序,然后调用一个fortran程序。然而,当我点击这个按钮时,它会打开程序,但是我正在调用的程序的菜单会进入无限循环......违规代码似乎在button1Click模块中。Tkinter python代码中的无限循环

任何帮助,非常感谢。

感谢

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

回答

0

什么让你觉得有一个无限循环发生了什么?我在button1Click中看不到任何循环,除非循环处于runiplanfile。您是否使用“无限循环”来表示GUI已停止响应?

Tkinter是单线程的,除了通过事件循环外无法处理事件。如果一个事件需要很长时间来处理,则GUI将挂起,直到完成该事件的处理。如果您正在执行一个外部进程并等待它完成,那么您的GUI看起来会被冻结,直到该进程结束。