目前我正在开发一个有两个按钮的 GUI:开始和停止。这个想法是,当用户按下 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 类中提前谢谢!