2016-04-28 24 views
0

我一直在爲我開發的基本Tkinter GUI添加GPIO物理按鈕功能。Tkinter和GPIO.add_event_detect - TypeError?

(GUI代碼是基於這個例子:http://pythonprogramming.net/change-show-new-frame-tkinter/

基於此提出,我已經添加了名爲「check_for_button_press」一個GPIO按鈕功能,它使用GPIO.add_event_detect和建議。經過

我已經調整了,但我一直在結束了與此錯誤:

TypeError: check_for_button_press() takes exactly 1 argument (2 given)

有明顯一些基本的東西,我很想念,任何人可以幫我鑑定這是什麼?謝謝!

這裏是新功能的代碼註釋:

import Tkinter as tk 
import time 
import RPi.GPIO as GPIO 
GPIO.setmode(GPIO.BCM) 
GPIO.setup(5, GPIO.IN, pull_up_down=GPIO.PUD_UP) 
GPIO.add_event_detect(5, GPIO.RISING) 

class SampleApp(tk.Tk): 

    def __init__(self, *args, **kwargs): 
     tk.Tk.__init__(self, *args, **kwargs) 
     self.attributes('-fullscreen', True) 
     self.config(cursor="none") 

     container = tk.Frame(self) 
     container.pack(side="top", fill="both", expand=True) 
     container.grid_rowconfigure(0, weight=0) 
     container.grid_columnconfigure(0, weight=0) 

     self.frames = {} 
     for F in (StartPage, PageOne): 
      page_name = F.__name__ 
      frame = F(container, self) 
      self.frames[page_name] = frame 
      frame.grid(row=0, column=0, sticky="nsew") 

     self.show_frame("StartPage") 


    def show_frame(self, page_name): 
     '''Show a frame for the given page name''' 
     frame = self.frames[page_name] 
     frame.tkraise() 

    def check_for_button_press(self):    #######check_for_button_press 
     if GPIO.event_detected(5):     #######GPIO 
      self.show_frame(page_name)    ####### 
     self.after(10, self.check_for_button_press) ####### 



class StartPage(tk.Frame): 

    def __init__(self, parent, controller): 
     tk.Frame.__init__(self, parent) 
     self.controller = controller 

     photonav1 = tk.PhotoImage(file="nav1.gif") 
     photonav2 = tk.PhotoImage(file="nav2.gif") 
     photonav3 = tk.PhotoImage(file="nav3.gif") 
     photonav4 = tk.PhotoImage(file="nav4.gif") 
     photonav5 = tk.PhotoImage(file="nav5.gif") 
     photonav6 = tk.PhotoImage(file="nav6.gif") 
     photosub1 = tk.PhotoImage(file="navmain1.gif") 


     buttonnav1 = tk.Button(self, image=photonav1, width=158, height=38, 
    text="", bg='green', bd=0, highlightthickness=0, 
          command=lambda: controller.show_frame("PageOne")) 

     buttonnav2 = tk.Button(self, image=photonav2, width=158, height=38, 
    text="", bg='green', bd=0, highlightthickness=0, activebackground="green", 
          command=lambda: controller.show_frame("PageTwo")) 

     buttonnav3 = tk.Button(self, image=photonav3, width=158, height=38, 
    text="", bg='green', bd=0, highlightthickness=0, activebackground="green", 
          command=lambda: controller.show_frame("PageThree")) 

     buttonnav4 = tk.Button(self, image=photonav4, width=158, height=38, 
    text="", bg='green', bd=0, highlightthickness=0, activebackground="green", 
          command=lambda: controller.show_frame("PageFour")) 

     buttonnav5 = tk.Button(self, image=photonav5, width=158, height=38, 
    text="", bg='green', bd=0, highlightthickness=0, activebackground="green", 
          command=lambda: controller.show_frame("PageFive")) 

     buttonnav6 = tk.Button(self, image=photonav6, width=158, height=38, 
    text="", bg='green', bd=0, highlightthickness=0, activebackground="green", 
          command=lambda: controller.show_frame("PageSix")) 

     buttonsub1 = tk.Button(self, image=photosub1, width=158, height=238, 
    text="", bg='green', bd=0, highlightthickness=0, activebackground="green", 
          command=lambda: controller.show_frame("PageSix")) 



     buttonsub1.pack(side=tk.RIGHT, fill=tk.Y, padx=0, pady=0) 
     buttonsub1.image = photosub1   

     buttonnav1.pack() 
     buttonnav1.image = photonav1 

     buttonnav2.pack() 
     buttonnav2.image = photonav2 

     buttonnav3.pack() 
     buttonnav3.image = photonav3 

     buttonnav4.pack() 
     buttonnav4.image = photonav4 

     buttonnav5.pack() 
     buttonnav5.image = photonav5 

     buttonnav6.pack() 
     buttonnav6.image = photonav6   


class PageOne(tk.Frame): 

# .... 
# ... 
# ... truncated 
# .. 
# . 



if __name__ == "__main__": 
    app = SampleApp() 
    app.after(0, app.check_for_button_press, app) 
    app.mainloop() 
+0

'def check_for_button_press(self):'內部不會'self.after(10,self.check_for_button_press)'創建一個無限遞歸?如果你的程序在2小時46分鐘後引發異常,那就是爲什麼(python的默認遞歸深度限制是1000,並且每次函數運行時你似乎都等待10秒...) – jDo

+0

謝謝,我會檢查一下。新的使用後方法 – naturesrat

+0

我的意思是你爲什麼要這樣做?它是否必須自稱?我對tkinter知之甚少,但看起來很奇怪。你有沒有嘗試刪除'self.after(10,self.check_for_button_press)'?任何改變? – jDo

回答

1

你爲什麼打電話check_for_button_press這樣開始?

app.after(0, app.check_for_button_press, app) 

你應該只調用

app.check_for_button_press() 

和後內check_for_button_press將處理由使用方法後,沒有必要在這裏兩次使用後不斷地檢查。

縱觀after method您可以傳遞:

after(delay_ms, callback, *args) 

所以,你確實是傳遞兩個參數。

+0

啊是的,現在它的工作,謝謝! – naturesrat