2

我以前从未使用过python,我希望有人可以用这个为我指明正确的方向......我想要一个带有2个窗口的gui,我找到了一个代码示例。我正在尝试将树莓派的 RPi.GPIO 集成到其中,这似乎运行良好,到目前为止我所管理的是:

http://pastebin.com/9aWDgg0r

问题是我无法访问文本小部件以从我的函数中添加文本,据我了解,这是因为我的函数不在我想要更改的小部件的类之外。那么如何从类外部引用一个小部件并进行更改呢?

def check_five(): 
    if (GPIO.input(25) == GPIO.HIGH):
        fiveTimes_out()
    log.insert('1.0', '5 button down')
    root.after(10,check_five)


class MyApp(object):
    """"""
    #----------------------------------------------------------------------
    def __init__(self, parent):

        """Constructor"""
        self.root = parent
        self.root.title("Main frame")
        self.frame = Tk.Frame(parent)
        self.frame.pack()

        ....

        log = Tk.Text(state='normal', width=70, height=10, wrap='none')
        log.place(x=40, y=160)   

log.insert('1.0', '5 button down') 应该类似于 MyApp.log.insert('1.0', '5 button down') 吗?

我可以将这些函数移到类中,但是我不确定如何让这些函数与 .after 一起运行或将它们放在哪里。

    root.after(10,check_five)
    root.after(10,check_two)
    root.after(10,check_one)
    root.after(10,check_toggle)

任何帮助都会很棒,谢谢。

4

2 回答 2

2
  • 制作check_five,check_twocheck_onecheck_toggle 方法MyApp
  • self.log = Tk.Text中定义MyApp.__init__。这样,其他方法MyApp就可以引用了self.log
  • if __name__ == "__main__":脚本的一部分,使用 app.check_five而不是check_five引用appcheck_five方法。对于其他check_*方法也是如此。

import Tkinter as Tk
import time
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)

# setup 5 output pin
GPIO.setup(11, GPIO.OUT)
# setup 2 output pin
GPIO.setup(14, GPIO.OUT)
# setup 1 output pin
GPIO.setup(15, GPIO.OUT)

# set low output states on start
GPIO.output(11, GPIO.LOW)
GPIO.output(14, GPIO.LOW)
GPIO.output(15, GPIO.LOW)

# setup 5 input pin
GPIO.setup(25, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)
# setup 2 input pin
GPIO.setup(24, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)
# setup 1 input pin
GPIO.setup(23, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)

# setup window toggle pin
GPIO.setup(22, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)


# out functions light certain led a number of times     
def fiveTimes_out():
    #1
    GPIO.output(11, GPIO.HIGH)
    time.sleep(0.200)
    GPIO.output(11, GPIO.LOW)
    time.sleep(0.200)
    #2
    GPIO.output(11, GPIO.HIGH)
    time.sleep(0.200)
    GPIO.output(11, GPIO.LOW)
    time.sleep(0.200)
    #3
    GPIO.output(11, GPIO.HIGH)
    time.sleep(0.200)
    GPIO.output(11, GPIO.LOW)
    time.sleep(0.200)
    #4
    GPIO.output(11, GPIO.HIGH)
    time.sleep(0.200)
    GPIO.output(11, GPIO.LOW)
    time.sleep(0.200)
    #5          
    GPIO.output(11, GPIO.HIGH)
    time.sleep(0.200)
    GPIO.output(11, GPIO.LOW)
    time.sleep(0.200)

def twoTimes_out():
    #1
    GPIO.output(14, GPIO.HIGH)
    time.sleep(0.200)
    GPIO.output(14, GPIO.LOW)
    time.sleep(0.200)
    #2
    GPIO.output(14, GPIO.HIGH)
    time.sleep(0.200)
    GPIO.output(14, GPIO.LOW)
    time.sleep(0.200)        

def oneTimes_out():
    #1
    GPIO.output(15, GPIO.HIGH)
    time.sleep(0.200)
    GPIO.output(15, GPIO.LOW)
    time.sleep(0.200)



########################################################################
class OtherFrame(Tk.Toplevel):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        Tk.Toplevel.__init__(self)
        self.geometry("640x480+0+0")
        self.configure(background = 'yellow')

        self.title("otherFrame")

########################################################################
class MyApp(object):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, parent):

        """Constructor"""
        self.root = parent
        self.root.title("Main frame")
        self.frame = Tk.Frame(parent)
        self.frame.pack()

        btn = Tk.Button(self.frame, text = "Other Window", command = self.openFrame)
        btn.pack()

        btn2 = Tk.Button(self.frame, text = "Function test", command = twoTimes_out)
        btn2.pack()

        titleLabel = Tk.Label(text = "My Label")
        titleLabel.place(x = 40, y = 60)

        insertLabel = Tk.Label(text = "Label")
        insertLabel.place(x = 170, y = 110)

        self.log = Tk.Text(state = 'normal', width = 70, height = 10, wrap = 'none')
        self.log.place(x = 40, y = 160)

        thanksLabel = Tk.Label(text = "Thank You!")
        thanksLabel.place(x = 70, y = 350)

        self.log.insert('1.0', 'here is my text to insert')


    #----------------------------------------------------------------------
    def hide(self):
        """"""
        self.root.withdraw()

    #----------------------------------------------------------------------
    def openFrame(self):
        """"""
        self.hide()
        subFrame = OtherFrame()
        handler = lambda: self.onCloseOtherFrame(subFrame)
        btn = Tk.Button(subFrame, text = "Close", command = handler)
        btn.pack()
        secondPageLabel = Tk.Label(text = "HI")
        secondPageLabel.place(x = 170, y = 110)

    #----------------------------------------------------------------------
    def onCloseOtherFrame(self, otherFrame):
        """"""
        otherFrame.destroy()
        self.show()

    #----------------------------------------------------------------------
    def show(self):
        """"""
        self.root.update()
        self.root.deiconify()

    # in functions check if buttons are pushed and run specific functions
    # also write messages to log    

    def check_five(self): 
        if (GPIO.input(25) == GPIO.HIGH):
            fiveTimes_out()
            self.log.insert('1.0', '5 button down')
        else:
            self.log.insert('1.0', '5 button up')
        root.after(10, self.check_five)

    def check_two(self): 
        if (GPIO.input(24) == GPIO.HIGH):
            twoTimes_out()
            self.log.insert('1.0', '2 button down')
        else:
            self.log.insert('1.0', '2 button up')
        root.after(10, self.check_five)

    def check_one(self): 
        if (GPIO.input(23) == GPIO.HIGH):
            oneTimes_out()
            self.log.insert('1.0', '1 button down')
        else:
            self.log.insert('1.0', '1 button up')
        root.after(10, self.check_five)

    # check if window toggle button is pushed
    # you reference self in check_toggle, so check_toggle should probably be a method.
    def check_toggle(self): 
        if (GPIO.input(22) == GPIO.HIGH):
            self.openFrame()                 
        root.after(10, check_toggle)

#----------------------------------------------------------------------
if __name__ == "__main__":
    root = Tk.Tk()

    root.geometry("640x480+0+0")
    root.configure(background = 'red')
    app = MyApp(root)
    root.after(10, app.check_five)
    root.after(10, app.check_two)
    root.after(10, app.check_one)
    root.after(10, app.check_toggle)
    root.mainloop()
于 2012-12-12T21:21:22.520 回答
1

__init__MyApp 的方法中,您需要将其存储log为实例变量 (do self.log = log),以便它在离开后仍然存在__init__。我建议做check_five一个 MyApp 的方法,在这种情况下你可以通过self.log. 如果你想保持check_five分开,那么你可以通过类似的东西来访问它myapp.log,你之前myapp通过做创建的地方myapp = MyApp(root)。在 MyApp 中使用 after 运行方法时,您可以访问它,例如self.check_five.

于 2012-12-12T21:09:02.887 回答