2014-10-18 52 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

相關問題