调用类时所有函数都在运行(tkinter)[重复]

Posted

技术标签:

【中文标题】调用类时所有函数都在运行(tkinter)[重复]【英文标题】:Functions all running when class is called (tkinter) [duplicate] 【发布时间】:2021-03-16 00:03:25 【问题描述】:

我目前正在 Tkinter 中构建一个应用程序,并将不同的页面放入类中以使其更易于管理。但是我一直遇到这个问题,当我调用我想要的函数时,即l.LoginPage(window).login(window),它会自动运行按下按钮时要触发的函数。

请看下面的代码:

from tkinter import *
import requests
import socket
from PIL import Image, ImageTk
import time


class LoginPage:
    def __init__(self, window):
        window.geometry("500x430")
        self.page = Frame(window)
        self.logo = PhotoImage(file="images\TheQALifeLogo.pgm")

        self.change_to = ""
        self.image = Label(window, image=self.logo, bg="black")
        self.text_entry_username = Entry(window, width=50, bg="white")
        self.text_entry_password = Entry(window, width=50, bg="white")
        self.Username_Label = Label(window, text="Username", bg="black", fg="white", font="none 12 bold")
        self.Password_Label = Label(window, text="Password", bg="black", fg="white", font="none 12 bold")
        self.Submit_Button = Button(window, text="SUBMIT", width=8, command=self.change_home(window))
        self.Create_Account = Button(window, text="Don't have an account?", width=18, command=self.change_create(window))
        self.login_result = Text(window, width=35, height=1, wrap=WORD, bg="black", fg="red", borderwidth=0)

    def change_page(self, change_to):
        username = self.text_entry_username.get()
        password = self.text_entry_password.get()

        hostname = socket.gethostname()
        ip_address = socket.gethostbyname(hostname)
        url = 'http://localhost:80'
        send_data = "LOGIN" + " " + username + " " + password + " " + ip_address
        print(send_data)
        x = requests.post(url, data=send_data)
        print(x.text)

        if x.text == "LOGIN True":
            print("We are returning the value TRUE")
            response = "TRUE"
        elif x.text == "LOGIN False":
            print("We are returning the value FALSE")
            response = "FALSE"

        if response == "TRUE":
            return self.change_to

        elif response == "FALSE":
            self.login_result.delete(0.0, END)
            self.login_result.insert(END, "Username or Password is incorrect")

    def change_home(self, window):
        self.change_to = "home"
        return LoginPage(window).change_page(self.change_to)

    def change_create(self, window):
        self.change_to = "create"
        return LoginPage(window).change_page(self.change_to)

    def login(self, window):
        window.configure(background="black")

        self.image.pack(side="top")
        self.Username_Label.place(x=220, y=260)
        self.Password_Label.place(x=220, y=310)
        self.text_entry_username.place(x=107, y=285)
        self.text_entry_password.place(x=107, y=335)
        self.Submit_Button.place(x=220, y=365)
        self.Create_Account.place(x=185, y=400)
        self.login_result.place(x=107, y=230)

        window.mainloop()


这导致函数调用函数的无限循环。这是错误信息;

    return LoginPage(window).change_page(self.change_to)
  File "C:\Users\Joshua Waghorn\OneDrive\GUI\loginpage.py", line 20, in __init__
    self.Submit_Button = Button(window, text="SUBMIT", width=8, command=self.change_home(window))
  File "C:\Users\Joshua Waghorn\OneDrive\GUI\loginpage.py", line 52, in change_home
    return LoginPage(window).change_page(self.change_to)
  File "C:\Users\Joshua Waghorn\OneDrive\GUI\loginpage.py", line 20, in __init__
    self.Submit_Button = Button(window, text="SUBMIT", width=8, command=self.change_home(window))
  File "C:\Users\Joshua Waghorn\OneDrive\GUI\loginpage.py", line 52, in change_home
    return LoginPage(window).change_page(self.change_to)

(这只是错误信息的一小部分,因为它一直在重复)

【问题讨论】:

【参考方案1】:

当您需要为 Button 命令提供参数时,您必须使用 lambda 或 partial 来防止它立即执行。

    self.Submit_Button = Button(window, text="SUBMIT", width=8, command=lambda: self.change_home(window))

虽然在这种情况下您根本不需要提供参数,因为“window”参数与“self.master”相同。

【讨论】:

好的,谢谢。我试试看 LoginPage 不是 tkinter 小部件类,因此它没有 master。因此self.master 未定义。

以上是关于调用类时所有函数都在运行(tkinter)[重复]的主要内容,如果未能解决你的问题,请参考以下文章

当在另一个页面上调用类时,私有函数__construct()不起作用[重复]

单击按钮后如何将输入值(TKinter)传递给其他类中的函数并使用传递的值运行函数[重复]

Python & Tkinter -> 关于调用冻结程序的长时间运行函数

所有 tkinter 函数在程序启动时运行

在ES6 JavaScript中调用类时,为什么我得到null值? [重复]

如果调用另一个函数,则不会加载 TkInter 帧