在某个点暂停代码

2024-04-19 04:40:54 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

Tags: thetextselfinputgpiobindbuttonglobal
1条回答
网友
1楼 · 发布于 2024-04-19 04:40:54

“暂停并等待”和“事件驱动GUI编程”不能同时使用。只要主GUI线程在等待某个东西时被阻塞,那么其他事件就不能被处理,程序就会被冻结。你的选择是改变你“等待”的方式(实际上不是等待)或者使用另一个线程。你知道吗

This answer另一个问题同样适用于这里,并将为您提供更多信息和指针。你知道吗

相关问题 更多 >