私は開発してきた基本的な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を使用して.afterだここでの提案に基づいて
私はそれを微調整してきたが、私は、このエラーで終わる続ける:
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()
def check_for_button_press(self)内の 'self.after(10、self.check_for_button_press)':無限再帰を作成しませんか?あなたのプログラムが2時間46分後に例外を発生させた場合、その理由は(pythonのデフォルトの再帰深度制限は1000で、関数が実行されるたびに10秒待っているようです...) – jDo
ありがとう、私はそれをチェックします。アフター・メソッドの使い方が初めてです – naturesrat
なぜ私はそれをやっているのですか?それは自分自身を呼び出す必要がありますか?私はtkinterについてもあまり知らないが、それは変わっているようだ。 'self.after(10、self.check_for_button_press)'を削除しようとしましたか?変更? – jDo