目前我正在开发一个有两个按钮的GUI:START和STOP。这个想法是,当用户按下START时,代码将打印用户在txt文件中输入的所有内容(实时),一旦按下STOP按钮,代码将停止打印进一步输入。 我的问题是:如果在按下按钮开始的情况下我使用循环“while True”,则会立即阻止GUI,因此阻止停止按钮的事件(代码进入无限循环)
self.close_signal=False
def Start(self,event):
path=os.chdir(self.pathsave)
with open(self.tFileName.GetValue()+".txt",'a') as f:
#This is the cycle I need to break when the button bStop (event Stop)
while True:
char=self.getch()
f.write(char)
if self.close_signal==True:
break
def Stop(self,event):
self.close_signal=True
f.close()
def getch(self):
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd , termios.TCSADRAIN , old_settings)
return ch
这段代码包含在我的wx.Frame的类中 提前谢谢!