如何创建一个根据开关状态一直更新的开关wxpython程序?
How to create a wxpython program for switch that update all the time according to the switch state?
我已经为 switch 创建了一个简单的 python 程序。
这个简单的程序工作正常。
代码如下:
import RPi.GPIO as GPIO
from time import sleep
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(40, GPIO.IN)
count = 0
flag = 0
while True:
input = GPIO.input(40)
if ((not flag) and input):
flag = input
count += 1
print "--------------------"
print "Button is pressed"
print "Flag = ", flag
print "Count = ", count
sleep(0.05)
elif ((not input) and flag):
flag = input
count += 1
print "--------------------"
print "Button is debounce"
print "Flag = ", flag
print "Count = ", count
sleep(0.05)
GPIO.cleanup()
但是当我尝试创建一个 wxPython 程序来显示输出时
该程序在第一次单击按钮时工作,但是当我释放按钮时,wxPython 程序上的输出保持不变,除了更新的时间在我释放按钮时停止并在我按下按钮时继续。
wxPython程序如下:
import RPi.GPIO as GPIO
import datetime
import os
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(40, GPIO.IN)
count = 0
flag = 0
button = "None"
input = GPIO.input(40)
global update_time
update_time = datetime.datetime.strftime(datetime.datetime.now(), '%d-%m-%y %H:%M:%S')
try:
import wx
except ImportError:
raise ImportError, "The wxPython module is required to run this program."
class OnOffApp_wx(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, size = (500, 200), title = 'ON / OFF Status')
self.SetBackgroundColour(wx.WHITE)
self.SetWindowStyle(wx.STAY_ON_TOP)
self.parent = parent
self.initialize()
def initialize(self):
sizer = wx.GridBagSizer()
font = wx.Font(20, wx.DECORATIVE, wx.ITALIC, wx.NORMAL)
self.SetFont(font)
self.label1 = wx.StaticText(self, -1, label=u'Button Status : {}'.format(button))
self.label1.SetBackgroundColour(wx.WHITE)
self.label1.SetForegroundColour(wx.BLACK)
sizer.Add(self.label1, (1,0), (1,2), wx.EXPAND)
self.label2 = wx.StaticText(self, -1, label=u'Flag Status : {}'.format(flag))
self.label2.SetBackgroundColour(wx.WHITE)
self.label2.SetForegroundColour(wx.BLACK)
sizer.Add(self.label2, (2,0), (1,3), wx.EXPAND)
self.label3 = wx.StaticText(self, -1, label=u'Count Status : {}'.format(count))
self.label3.SetBackgroundColour(wx.WHITE)
self.label3.SetForegroundColour(wx.BLACK)
sizer.Add(self.label3, (3,0), (1,4), wx.EXPAND)
self.label4 = wx.StaticText(self, -1, label=u'Time Updated : {}'.format(update_time))
self.label4.SetBackgroundColour(wx.WHITE)
self.label4.SetForegroundColour(wx.BLACK)
sizer.Add(self.label4, (4,0), (1,5), wx.EXPAND)
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.on_timer, self.timer)
self.timer.Start(50)
self.SetSizer(sizer)
self.Show(True)
def on_timer(self,event):
global update_time
update_time = datetime.datetime.strftime(datetime.datetime.now(), '%d-%m-%y %H:%M:%S')
count = 0
flag = 0
input = GPIO.input(40)
if ((not flag) and input):
flag = input
count += 1
button = 'Pressed'
#self.label1.SetLabel("Button Status : Pressed")
self.label1.SetLabel("Button Status : {}".format(button))
self.label2.SetLabel("Flag Status : {}".format(flag))
self.label3.SetLabel("Count Status : {}".format(count))
self.label4.SetLabel("Time Updated : {}".format(update_time))
elif ((not input) and flag):
flag = input
count += 1
button = 'Debounce'
#self.label1.SetLabel("Button Status : Debounce")
self.label1.SetLabel("Button Status : {}".format(button))
self.label2.SetLabel("Flag Status : {}".format(flag))
self.label3.SetLabel("Count Status : {}".format(count))
self.label4.SetLabel("Time Updated : {}".format(update_time))
if __name__ == "__main__":
Rs = wx.App()
OnOffApp_wx(None, -1, 'ON / OFF Status')
Rs.MainLoop()
GPIO.cleanup()
帮助我使 wxPython 程序工作,当我按下按钮时,输入状态和标志变为 1,计数增加 1。当我松开按钮时,输入状态和标志变为 0,计数增加 1。
在 on_timer
中,您每 50 毫秒将 count
和 flag
都重新设置为零。
要么将它们作为变量传递到函数中,要么为了您的目的,在 on_timer
函数中将它们简单地声明为 global
会更容易。
所以:
count = 0
flag = 0
变成:
global count, flag
我已经为 switch 创建了一个简单的 python 程序。 这个简单的程序工作正常。 代码如下:
import RPi.GPIO as GPIO
from time import sleep
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(40, GPIO.IN)
count = 0
flag = 0
while True:
input = GPIO.input(40)
if ((not flag) and input):
flag = input
count += 1
print "--------------------"
print "Button is pressed"
print "Flag = ", flag
print "Count = ", count
sleep(0.05)
elif ((not input) and flag):
flag = input
count += 1
print "--------------------"
print "Button is debounce"
print "Flag = ", flag
print "Count = ", count
sleep(0.05)
GPIO.cleanup()
但是当我尝试创建一个 wxPython 程序来显示输出时 该程序在第一次单击按钮时工作,但是当我释放按钮时,wxPython 程序上的输出保持不变,除了更新的时间在我释放按钮时停止并在我按下按钮时继续。 wxPython程序如下:
import RPi.GPIO as GPIO
import datetime
import os
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(40, GPIO.IN)
count = 0
flag = 0
button = "None"
input = GPIO.input(40)
global update_time
update_time = datetime.datetime.strftime(datetime.datetime.now(), '%d-%m-%y %H:%M:%S')
try:
import wx
except ImportError:
raise ImportError, "The wxPython module is required to run this program."
class OnOffApp_wx(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, size = (500, 200), title = 'ON / OFF Status')
self.SetBackgroundColour(wx.WHITE)
self.SetWindowStyle(wx.STAY_ON_TOP)
self.parent = parent
self.initialize()
def initialize(self):
sizer = wx.GridBagSizer()
font = wx.Font(20, wx.DECORATIVE, wx.ITALIC, wx.NORMAL)
self.SetFont(font)
self.label1 = wx.StaticText(self, -1, label=u'Button Status : {}'.format(button))
self.label1.SetBackgroundColour(wx.WHITE)
self.label1.SetForegroundColour(wx.BLACK)
sizer.Add(self.label1, (1,0), (1,2), wx.EXPAND)
self.label2 = wx.StaticText(self, -1, label=u'Flag Status : {}'.format(flag))
self.label2.SetBackgroundColour(wx.WHITE)
self.label2.SetForegroundColour(wx.BLACK)
sizer.Add(self.label2, (2,0), (1,3), wx.EXPAND)
self.label3 = wx.StaticText(self, -1, label=u'Count Status : {}'.format(count))
self.label3.SetBackgroundColour(wx.WHITE)
self.label3.SetForegroundColour(wx.BLACK)
sizer.Add(self.label3, (3,0), (1,4), wx.EXPAND)
self.label4 = wx.StaticText(self, -1, label=u'Time Updated : {}'.format(update_time))
self.label4.SetBackgroundColour(wx.WHITE)
self.label4.SetForegroundColour(wx.BLACK)
sizer.Add(self.label4, (4,0), (1,5), wx.EXPAND)
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.on_timer, self.timer)
self.timer.Start(50)
self.SetSizer(sizer)
self.Show(True)
def on_timer(self,event):
global update_time
update_time = datetime.datetime.strftime(datetime.datetime.now(), '%d-%m-%y %H:%M:%S')
count = 0
flag = 0
input = GPIO.input(40)
if ((not flag) and input):
flag = input
count += 1
button = 'Pressed'
#self.label1.SetLabel("Button Status : Pressed")
self.label1.SetLabel("Button Status : {}".format(button))
self.label2.SetLabel("Flag Status : {}".format(flag))
self.label3.SetLabel("Count Status : {}".format(count))
self.label4.SetLabel("Time Updated : {}".format(update_time))
elif ((not input) and flag):
flag = input
count += 1
button = 'Debounce'
#self.label1.SetLabel("Button Status : Debounce")
self.label1.SetLabel("Button Status : {}".format(button))
self.label2.SetLabel("Flag Status : {}".format(flag))
self.label3.SetLabel("Count Status : {}".format(count))
self.label4.SetLabel("Time Updated : {}".format(update_time))
if __name__ == "__main__":
Rs = wx.App()
OnOffApp_wx(None, -1, 'ON / OFF Status')
Rs.MainLoop()
GPIO.cleanup()
帮助我使 wxPython 程序工作,当我按下按钮时,输入状态和标志变为 1,计数增加 1。当我松开按钮时,输入状态和标志变为 0,计数增加 1。
在 on_timer
中,您每 50 毫秒将 count
和 flag
都重新设置为零。
要么将它们作为变量传递到函数中,要么为了您的目的,在 on_timer
函数中将它们简单地声明为 global
会更容易。
所以:
count = 0
flag = 0
变成:
global count, flag