0

I am currently building an application in Tkinter, and have been putting the different pages into classes to make them easier to manage. But I have been having this problem, when I call the function I want i.e. l.LoginPage(window).login(window) it will automatically run the function that is meant to be triggered when the button is pressed.

Please see the code bellow:

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()


This results in this endless loop of functions calling functions. This is the error message;

    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)

(This is only a small portion of the error message, because it keeps on repeating)

Jacob Ward
  • 221
  • 2
  • 12

1 Answers1

1

When you need to provide arguments to a Button command, you must use lambda or partial to prevent it from executing immediately.

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

Although in this case you don't need to provide arguments at all, since the 'window' argument is the same as 'self.master'.

Novel
  • 13,406
  • 2
  • 25
  • 41