Итак, у меня есть этот код, запускающий несколько окон GUI в tkinter, у меня также есть второй код, который содержит определенную функцию, назначенную кнопке, которая, похоже, не работает. Я очень устал и не могу найти решение, я уверен, что это одна из основных вещей. Я имею в виду функцию toggle_text1 в классе PageOne. Буду признателен за каждую помощь, спасибо!Назначение функции кнопке не работает
import Tkinter as tk
TITLE_FONT = ("Helvetica", 18, "bold")
class SampleApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
# the container is where we'll stack a bunch of frames
# on top of each other, then the one we want visible
# will be raised above the others
container = tk.Frame(self)
container.pack(side="top", fill="both", expand=False)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (StartPage, PageOne, PageTwo, Page3, Page4):
frame = F(container, self)
self.frames[F] = frame
# put all of the pages in the same location;
# the one on the top of the stacking order
# will be the one that is visible.
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, c):
'''Show a frame for the given class'''
frame = self.frames[c]
frame.tkraise()
def toggle_text1():
if button1["text"] == "WL":
button1["text"] = "WYL"
label1["bg"] = "green"
#wiringpi.pinMode(91,0)
#wiringpi.digitalWrite(91,0)
else:
button1["text"] = "WL"
label1["bg"] = "red"
#wiringpi.pinMode(91,1)
#wiringpi.digitalWrite(91,0)
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="This is the start page", font=TITLE_FONT)
label.pack(side="left", fill="x", pady=10)
button1 = tk.Button(self, text="Go to Page One",
command=lambda: controller.show_frame(PageOne))
button2 = tk.Button(self, text="Go to Page Two",
command=lambda: controller.show_frame(PageTwo))
button3 = tk.Button(self, text="Go to Page 3",
command=lambda: controller.show_frame(Page3))
button4 = tk.Button(self, text="Go to Page 4",
command=lambda: controller.show_frame(Page4))
button1.pack(pady=10)
button2.pack(pady=10)
button3.pack(pady=10)
button4.pack(pady=10)
О, дерьмо, я удалил виджет раньше и забыл вставить его снова, мой плохой. Как насчет сейчас?
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="This is page 1", font=TITLE_FONT)
label.pack(side="top", fill="x", pady=10)
button = tk.Button(self, text="Go to the start page",
command=lambda: controller.show_frame(StartPage))
button1 = tk.Button(self, text='WL', command=toggle_text1)
button.pack()
button1.pack()
label1.pack()
if __name__ == "__main__":
app = SampleApp()
app.mainloop()
* «Я очень устал и не могу найти решение, я уверен, что это какая-то основная вещь», - тогда ложитесь спать и повторите попытку утром. SO здесь не для базовой отладки для вас. – jonrsharpe
Когда я ищу 'toggle_text1', я не вижу, чтобы вы фактически использовали эту функцию с помощью кнопки. –
Извините, мой плохой я удалил виджет, и теперь его перепроданность, вы можете проверить его сейчас? –