Python 2.7 Python-Tkinter在子窗口上设置退出按钮的正确方法以及它如何影响切换按钮

Python 2.7 Python-Tkinter在子窗口上设置退出按钮的正确方法以及它如何影响切换按钮,python-2.7,tkinter,raspberry-pi,exit,togglebutton,Python 2.7,Tkinter,Raspberry Pi,Exit,Togglebutton,我是一个试图用树莓圆周率学习Python的新手。我一直在写一些代码,试图为piFace插件板制作一个简单的模拟器 这其中有一些问题,我在努力解决这些问题的同时也在学习 该代码打开一个主窗口,显示八个切换按钮,每个按钮切换LED的开/关。我还添加了一个打开子窗口的按钮。 子窗口有两个按钮。一个是开/关切换按钮,使8个LED像骑士一样来回流动,另一个是退出按钮 我的问题是,如果我使用退出按钮,当LED来回流动时,子窗口会关闭,这是应该的。但是,如果我重新打开子窗口并使用切换按钮打开流式LED,则什么

我是一个试图用树莓圆周率学习Python的新手。我一直在写一些代码,试图为piFace插件板制作一个简单的模拟器

这其中有一些问题,我在努力解决这些问题的同时也在学习

该代码打开一个主窗口,显示八个切换按钮,每个按钮切换LED的开/关。我还添加了一个打开子窗口的按钮。 子窗口有两个按钮。一个是开/关切换按钮,使8个LED像骑士一样来回流动,另一个是退出按钮

我的问题是,如果我使用退出按钮,当LED来回流动时,子窗口会关闭,这是应该的。但是,如果我重新打开子窗口并使用切换按钮打开流式LED,则什么也不会发生。如果我再次按下切换按钮,LED开始正常发光

我有点理解问题所在。因为我在LED发光时关闭窗口,所以切换按钮状态仍处于打开状态。当我重新打开窗口并单击切换按钮时,我只是将切换按钮状态设置为关闭

我不知道如何解决这个问题。我是否应该以另一种可能正确的方式来看待关闭窗口?我应该看看一种预设拨动开关状态的方法吗?我应该试试完全不同的吗?我应该完全停止吗?:-)

我希望这有点道理

谢谢你的帮助

这是我的密码

# 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()

如果您希望在打开新窗口时将LED设置为off,则可以使用class属性并根据需要进行设置。在这个例子中,所有多余的积垢都被清除了。如果这不是你想要的,然后发回

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

你可以试着提出你的例子的一个提炼版本。由于依赖关系,大多数人无法运行此代码。试着创建一个最小的例子来演示这个问题。你的代码比它需要的复杂得多。例如,您不需要线程来完成您要做的事情。Tkinter小部件在之后有一个名为
的方法,可用于动画或按计划调用函数。@Aivar是的,我将制作一个抽象版本并发布它。谢谢你的建议。@Bryan Oakley是的,我想这比它需要的更复杂,但我正在学习,他们似乎有很多方法来做事情,线程是我在这个例子中尝试过的方法,但我会研究你提到的“之后”的事情。谢谢你的建议。我采纳了其中一位评论者的建议,对我的问题进行了提炼,并将其发布。谢谢你的例子。作为一个新手,我花了一点时间来分解它并理解它是如何工作的。