【问题标题】:Pause the code at some point在某个时候暂停代码
【发布时间】:2016-07-20 12:41:36
【问题描述】:

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

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

【问题讨论】:

    标签: python timer wxpython


    【解决方案1】:

    “暂停和等待”和“事件驱动的 GUI 编程”不能并存。只要主 GUI 线程被阻塞等待某事,那么其他事件就无法处理,程序就会出现冻结。您的选择是更改您“等待”的方式(实际上不等待)或使用另一个线程。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-19
      • 2013-05-05
      • 1970-01-01
      相关资源
      最近更新 更多