在某些时候暂停代码

时间:2016-07-20 12:41:37

标签: python timer wxpython

我想在准确的位置暂停代码并等待不同的输入加上点击的开始按钮。但是,如果无法实现,如何在开始按钮中添加另一个按钮以使其起作用?

import wx

import time

import RPi.GPIO as GPIO

global Total_Length
Total_Length = 500

global Input_Length
Input_Length = 0

a = 0


class tyler(wx.Frame):

    def __init__(self,parent,id):
        wx.Frame.__init__(self,parent,id,'Lyquid Crystal Laser Control',size=(500,200))
        panel=wx.Panel(self)

        #global Text_1
        self.Text_1 = wx.TextCtrl(panel,-1,"0",(350,30),(50,30))
        self.Text_1.Bind(wx.EVT_TEXT_ENTER,self.Start)
        self.Text_1.Bind(wx.EVT_KILL_FOCUS,self.Start)


        self.timer = wx.Timer(self)
        #self.Bind(wx.EVT_TIMER, self.timer)
        button_1=wx.Button(panel,label="Start",pos=(400,80),size=(80,30))
        button_2=wx.Button(panel,label="Stop",pos=(400,120),size=(80,30))
        self.Bind(wx.EVT_BUTTON, self.Start, button_1)
        #self.Bind(wx.EVT_BUTTON, self.Stop, button_2)

    def Start(self,event):
        global a
        Input_Length=float(self.Text_1.GetValue())
        #print(Input_Length)
        #a = Input_Length
        #print(Input_Length)
        dc=float(100*Input_Length/Total_Length)
        GPIO.setmode(GPIO.BCM)
        GPIO.setup(18,GPIO.OUT)
        GPIO.setwarnings(False)
        p = GPIO.PWM(18,1150)
        p.start(0)
        p.ChangeDutyCycle(dc)
        p.ChangeFrequency(1150)
            #I wanted to pause the code at here, until the input changes, and the start button clicked, so I add timer in below, however, the output is only a pulse but the square wave is what I wanted 
        if a == dc:
            self.timer.Start(1000)
        else:
            a = dc
            self.timer.Stop()
            #def Stop(self,event):
            GPIO.cleanup()



if __name__=='__main__':
    app=wx.PySimpleApp()
    frame=tyler(parent=None,id=-1)
    frame.Show()
    app.MainLoop()

1 个答案:

答案 0 :(得分:0)

“暂停和等待”和“事件驱动的GUI编程”不能一起使用。只要主GUI线程被阻塞等待某事,就无法处理其他事件并且程序似乎被冻结。您可以选择更改“等待”的方式(实际上不等待)或使用其他线程。

This answer另外一个问题同样适用于此,并会为您提供更多信息和指示。