2014-10-18 21 views
0

我是一名尝试使用Raspberry Pi学习Python的新手。我一直在编写一些代码来尝试为piFace添加一个简单的模拟器。Python - Tkinter在子窗口上设置退出按钮的正确方法以及它如何影响切换按钮

有几个问题,我正在学习,因为我工作的方式通过他们。

该代码打开一个主窗口并显示八个切换按钮,每个切换按钮可打开/关闭LED。我还添加了一个打开子窗口的按钮。 子窗口有两个按钮。一个是打开/关闭切换按钮,可以像骑士骑士一样前后来回传送8个LED,另一个是退出按钮。

我的问题是,如果我使用“退出”按钮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() 
+0

您可以尝试呈现示例的蒸馏版本。由于依赖关系,大多数人无法运行此代码。尝试创建一个演示问题的最小示例。 – Aivar 2014-10-18 19:00:20

+0

你的代码比需要的更复杂。例如,你不需要线程来完成你想要做的事情。 Tkinter窗口小部件有一个名为'after'的方法,可用于按计划进行动画或调用功能。 – 2014-10-18 22:54:13

+0

@Aivar是的,我会制作一个蒸馏版本并发布。感谢您的建议。 – 2014-10-19 07:43:12

回答

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

我接受了其中一位评论者的建议,并创建了一个我的问题的蒸馏版本,并将其发布。感谢这个例子。作为新手,需要我花一些时间来分解它,并了解它是如何工作的。 – 2014-10-28 12:59:53

相关问题