我是一个尝试用Raspberry Pi学习Python的新手。我一直在编写一些代码,试图为piFace add on on制作一个简单的模拟器。
它有一些问题,我在学习的过程中学习。
代码打开一个主窗口,显示八个切换按钮,每个按钮用于打开/关闭LED。我还添加了一个打开子窗口的按钮。 子窗口有两个按钮。一个是开/关切换按钮,使得8个LED的流像Knight Rider一样来回流动,另一个是退出按钮。
我的问题是,如果我使用退出按钮,LED会来回流动,子窗口会关闭。但是,如果我重新打开子窗口并使用切换按钮打开流式LED指示灯,则不会发生任何事情。如果我再次按下切换按钮,LED将开始正常流式传输。
我有点明白问题是什么。因为当LED正在流动时关闭窗口,切换按钮状态仍然处于ON状态。并且,当我重新打开窗口并单击切换按钮时,我只需将切换按钮状态设置为OFF。
我不确定如何解决这个问题。我应该看一下不同且可能正确的方式关闭窗口吗?我应该看一种预设拨动开关状态的方法吗?我应该尝试完全不同的东西吗?我应该完全停止吗? : - )
我希望这是有道理的。
感谢您的帮助。
这是我的代码......
# Idle 10_01_2014_GUI label image toggle
from time import sleep
import piface.pfio as pfio
pfio.init()
from Tkinter import *
import Tkinter as tk
import threading
class App:
def __init__(self, master):
self.master=master
frame = Frame(master)
frame.pack()
Label(frame, text='Turn LED ON').grid(row=0, column=0)
Label(frame, text='Turn LED OFF').grid(row=0, column=1)
self.button0 = Button(frame, text='LED 0 OFF', command=self.convert0)
self.button0.grid(row=2, column=0)
self.LED0 = Label(frame, image=logo2)
self.LED0.grid(row=2, column=1)
self.button1 = Button(frame, text='LED 1 OFF', command=self.convert1)
self.button1.grid(row=3, column=0)
self.LED1 = Label(frame, image=logo2)
self.LED1.grid(row=3, column=1)
self.button2 = Button(frame, text='LED 2 OFF', command=self.convert2)
self.button2.grid(row=4, column=0)
self.LED2 = Label(frame, image=logo2)
self.LED2.grid(row=4, column=1)
self.button3 = Button(frame, text='LED 3 OFF', command=self.convert3)
self.button3.grid(row=5, column=0)
self.LED3 = Label(frame, image=logo2)
self.LED3.grid(row=5, column=1)
self.button4 = Button(frame, text='LED 4 OFF', command=self.convert4)
self.button4.grid(row=6, column=0)
self.LED4 = Label(frame, image=logo2)
self.LED4.grid(row=6, column=1)
self.button5 = Button(frame, text='LED 5 OFF', command=self.convert5)
self.button5.grid(row=7, column=0)
self.LED5 = Label(frame, image=logo2)
self.LED5.grid(row=7, column=1)
self.button6 = Button(frame, text='LED 6 OFF', command=self.convert6)
self.button6.grid(row=8, column=0)
self.LED6 = Label(frame, image=logo2)
self.LED6.grid(row=8, column=1)
self.button7 = Button(frame, text='LED 7 OFF', command=self.convert7)
self.button7.grid(row=9, column=0)
self.LED7 = Label(frame, image=logo2)
self.LED7.grid(row=9, column=1)
self.buttonnewwindow = Button(frame, text='Knight Rider TEST', command=self.new_window)
self.buttonnewwindow.grid(row=10, column=0)
self.button8 = Button(frame, text='Exit', command=quit)
self.button8.grid(row=11, column=0)
def convert0(self, tog=[0]):
tog[0] = not tog[0]
if tog[0]:
print('LED 0 ON')
self.button0.config(text='LED 0 ON')
self.LED0.config(image = logo)
self.LED0.grid(row=2, column=2)
pfio.digital_write(0,1) #turn on
else:
print('LED 0 OFF')
self.button0.config(text='LED 0 OFF')
self.LED0.config(image = logo2)
self.LED0.grid(row=2, column=1)
pfio.digital_write(0,0) #turn off
def convert1(self, tog=[0]):
tog[0] = not tog[0]
if tog[0]:
print('LED 1 ON')
self.button1.config(text='LED 1 ON')
self.LED1.config(image = logo)
pfio.digital_write(1,1) #turn on
else:
print('LED 1 OFF')
self.button1.config(text='LED 1 OFF')
self.LED1.config(image = logo2)
pfio.digital_write(1,0) #turn off
def convert2(self, tog=[0]):
tog[0] = not tog[0]
if tog[0]:
print('LED 2 ON')
self.button2.config(text='LED 2 ON')
self.LED2.config(image = logo)
pfio.digital_write(2,1) #turn on
else:
print('LED 2 OFF')
self.button2.config(text='LED 2 OFF')
self.LED2.config(image = logo2)
pfio.digital_write(2,0) #turn off
def convert3(self, tog=[0]):
tog[0] = not tog[0]
if tog[0]:
print('LED 3 ON')
self.button3.config(text='LED 3 ON')
self.LED3.config(image = logo)
pfio.digital_write(3,1) #turn on
else:
print('LED 3 OFF')
self.button2.config(text='LED 3 OFF')
self.LED3.config(image = logo2)
pfio.digital_write(3,0) #turn off
def convert4(self, tog=[0]):
tog[0] = not tog[0]
if tog[0]:
print('LED 4 ON')
self.button4.config(text='LED 4 ON')
self.LED4.config(image = logo)
pfio.digital_write(4,1) #turn on
else:
print('LED 4 OFF')
self.button4.config(text='LED 4 OFF')
self.LED4.config(image = logo2)
pfio.digital_write(4,0) #turn off
def convert5(self, tog=[0]):
tog[0] = not tog[0]
if tog[0]:
print('LED 5 ON')
self.button5.config(text='LED 5 ON')
self.LED5.config(image = logo)
pfio.digital_write(5,1) #turn on
else:
print('LED 5 OFF')
self.button5.config(text='LED 5 OFF')
self.LED5.config(image = logo2)
pfio.digital_write(5,0) #turn off
def convert6(self, tog=[0]):
tog[0] = not tog[0]
if tog[0]:
print('LED 6 ON')
self.button6.config(text='LED 6 ON')
self.LED6.config(image = logo)
pfio.digital_write(6,1) #turn on
else:
print('LED 6 OFF')
self.button6.config(text='LED OFF')
self.LED6.config(image = logo2)
pfio.digital_write(6,0) #turn off
def convert7(self, tog=[0]):
tog[0] = not tog[0]
if tog[0]:
print('LED 7 ON')
self.button7.config(text='LED 7 ON')
self.LED7.config(image = logo)
pfio.digital_write(7,1) #turn on
else:
print('LED 7 OFF')
self.button7.config(text='LED OFF')
self.LED7.config(image = logo2)
pfio.digital_write(7,0) #turn off
def new_window(self):
print('New Window')
self.newWindow = tk.Toplevel(self.master)
self.app = App2(self.newWindow)
self.newWindow.grab_set() # I added this line to stop opening multiple new windows
class App2:
def __init__(self, master):
self.signal = False #added to stop thread
print('self.signal', self.signal)
self.master=master # I added this line to make the exit button work
frame = Frame(master)
frame.pack()
Label(frame, text='Turn LED ON').grid(row=0, column=0)
Label(frame, text='Turn LED OFF').grid(row=0, column=1)
self.button0 = Button(frame, text='Knight Rider OFF', command=self.convert0)
self.button0.grid(row=2, column=0)
self.LED0 = Label(frame, image=logo2)
self.LED0.grid(row=2, column=1)
self.button9 = Button(frame, text='Exit', command=self.close_window)
self.button9.grid(row=3, column=0)
def convert0(self, tog=[0]):
tog[0] = not tog[0]
if tog[0]:
print('Knight Rider ON')
self.button0.config(text='Knight Rider ON')
t=threading.Thread(target=self.LED)
t.start()
self.signal = True #added to stop thread
print('self.signal', self.signal)
print('tog[0]', tog[0])
self.LED0.config(image = logo)
else:
print('Knight Rider OFF')
self.button0.config(text='Knight Rider OFF')
self.signal = False #added to stop thread
print('self.signal', self.signal)
print('tog[0]', tog[0])
self.LED0.config(image = logo2)
def LED(self):
while self.signal: #added to stop thread
a=0
while self.signal: #added to stop thread
pfio.digital_write(a,1) #turn on
sleep(0.05)
pfio.digital_write(a,0) #turn off
sleep(0.05)
a=a+1
if a==7:
break
while self.signal: #added to stop thread
pfio.digital_write(a,1) #turn on
sleep(0.05)
pfio.digital_write(a,0) #turn off
sleep(0.05)
a=a-1
if a==0:
break
def close_window(self):
print('Knight Rider OFF')
print('self.signal', self.signal)
self.button0.config(text='Knight Rider OFF')
self.LED0.config(image = logo2)
self.signal = False #added to stop thread
print('self.signal', self.signal)
sleep(1)
print('Close Child window')
self.master.destroy() # I added this line to make the exit button work
root = Tk()
logo2 = PhotoImage(file="/home/pi/Off LED.gif")
logo = PhotoImage(file="/home/pi/Red LED.gif")
root.wm_title('LED on & off program')
app = App(root)
root.mainloop()
答案 0 :(得分:0)
如果您希望在打开新窗口时将LED设置为关闭,则可以使用类属性并根据需要进行设置。这个例子,删除了所有额外的残余,这样做。如果这不是你想要的,那么回发。
class App:
def __init__(self):
master=Tk()
self.master=master
frame = Frame(master)
frame.grid()
Label(frame, text='Turn LED ON').grid(row=0, column=0)
Label(frame, text='Turn LED OFF').grid(row=0, column=1)
self.toggle=0
self.newWindow=False
self.button0 = Button(frame, text='LED 0 OFF', command=self.convert0)
self.button0.grid(row=2, column=0)
self.LED0 = Label(frame, text="Label")
self.LED0.grid(row=2, column=1)
self.buttonnewwindow = Button(frame, text='New Window',
command=self.new_window)
self.buttonnewwindow.grid(row=10, column=0)
self.button8 = Button(frame, text='Exit', command=quit)
self.button8.grid(row=11, column=0)
master.mainloop()
def convert0(self):
self.toggle=not self.toggle
if self.toggle:
print('LED 0 ON')
self.button0.config(text='LED 0 ON')
##self.LED0.config(image = logo)
##self.LED0.grid(row=2, column=2)
##pfio.digital_write(0,1) #turn on
else:
print('LED 0 OFF')
self.button0.config(text='LED 0 OFF')
##self.LED0.config(image = logo2)
##self.LED0.grid(row=2, column=1)
##pfio.digital_write(0,0) #turn off
def new_window(self):
print('New Window')
if not self.newWindow:
self.newWindow = Toplevel(self.master)
Button(self.newWindow, text="Close Window", command=self.newWindow.destroy).grid()
##self.app = App2(self.newWindow)
##self.newWindow.grab_set() # I added this line to stop opening multiple new windows
self.toggle=1
self.convert0()
self.newWindow=False
App()