【发布时间】:2014-12-14 01:00:32
【问题描述】:
我是一名新手,正在尝试使用 Raspberry Pi 学习 Python。我一直在编写一些代码来尝试为板载 piFace 添加一个简单的模拟器。
它存在一些问题,我正在努力解决这些问题。
代码打开一个主窗口并显示八个切换按钮,每个按钮可以打开/关闭 LED。我还添加了一个打开子窗口的按钮。 子窗口有两个按钮。一个是开/关切换按钮,使 8 个 LED 像 Knight Rider 一样来回闪烁,另一个是退出按钮。
我的问题是,如果我使用退出按钮,当 LED 来回流动时,子窗口会按原样关闭。但是,如果我重新打开子窗口并使用切换按钮打开流式 LED,则什么也不会发生。如果我再次按下切换按钮,LED 将开始正常流式传输。
我有点理解问题所在。因为我在 LED 流式传输时关闭了窗口,所以切换按钮状态仍处于打开状态。而且,当我重新打开窗口并单击切换按钮时,我只是将切换按钮状态设置为 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()
【问题讨论】:
-
您可以尝试展示您的示例的精简版本。由于依赖关系,大多数人无法运行此代码。尝试创建一个演示问题的最小示例。
-
您的代码比它需要的复杂。例如,您不需要线程来执行您正在尝试做的事情。 Tkinter 小部件有一个名为
after的方法,可用于动画或按计划调用函数。 -
@Aivar 是的,我会制作一个精简版并发布。感谢您的建议。
-
@Bryan Oakley 是的,我想它比它需要的更复杂,但我正在学习,他们似乎有很多做事的方法,线程是我在这种情况下尝试的方法,但是我会调查你提到的“之后”的事情。感谢您的建议。
标签: python-2.7 tkinter raspberry-pi exit togglebutton