简体   繁体   中英

GUI Python Login Error: Missing an Argument

I am having an issue with this GUI login I am trying to make. Here is the code:

from tkinter import *

class OneBiggerWindow():
    def __init__(self, master):

        self.master = master

        master.label_usr = Label(text = 'Enter Username')
        master.label_usr.pack()

        master.entry_usr = Entry(master)
        master.entry_usr.pack()

        master.label_pswrd = Label(text = 'Enter Password')
        master.label_pswrd.pack()

        master.entry_pswrd = Entry(master)
        master.entry_pswrd.pack(padx = 15)

        master.submit = Button(text = 'Submit', command = self.Pswrd_Chkr)
        master.submit.pack(pady = 10)

    def Pswrd_Chkr(self, master):
        username = master.entry_pswrd.get()
        password = master.entry_usr.get()
        if username == 'BigBoy' and password == 55595:
            print('You got it')
        else:
            print('Boohoo, you are incorrect')

root = Tk()
GuiBoi = OneBiggerWindow(root)
root.mainloop()

So the little window opens and I type in the username and password and I simply get this error:

File "C:\Users\MyName\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 1699, in __call__
    return self.func(*args)
TypeError: Pswrd_Chkr() missing 1 required positional argument: 'master'

I have no clue how to fix this. Any help would be much appreciated! Thank you very much and happy holidays!

Since you went with OOP as a paradigm for developing your user interfaces, you should have fully utilised all the benefits it has to offer. However, something tells me you're not quite familiar with classes, objects, instance variables and other OOP concepts.

  1. I suggest you design your class to actually inherit from tkinter.Tk , since it makes sense that state and behavior of the user interface parts ( widgets ) are grouped into separate objects, which are all in turn in a separate namespace.

  2. Apart from that, I also suggest you get rid of from tkinter import * , since you don't know what names that imports. It can replace names you imported earlier, and it makes it very difficult to see where names in your program are supposed to come from. Use import tkinter as tk instead, which is kind of an idiom here in Python.

  3. You might want to read what PEP8 says when it comes to naming conventions ,spaces around keyword arguments , and the rest there is to the Python coding style guide in general:

  4. You had other minor issues in the code - for instance, you were doing object equality checking between two different data types, small typos, etc.

In any case, here's my view how you could slightly rewrite the code above:

import tkinter as tk

class Application(tk.Tk):

    def __init__(self):
        super().__init__()
        self._create_widgets()

    def _create_widgets(self):
        """
        Creating all root window widgets and configuring them properly
        """
        label_usr = tk.Label(text='Enter Username')
        label_usr.pack()
        self.entry_usr = tk.Entry(self)
        self.entry_usr.pack()
        label_pswrd = tk.Label(self, text='Enter Password')
        label_pswrd.pack()
        self.entry_pswrd = tk.Entry(self)
        self.entry_pswrd.pack(padx=15)
        submit = tk.Button(text='Submit', command=self.pswrd_chkr)
        submit.pack(pady=10)

    def pswrd_chkr(self):
        username = self.entry_usr.get()
        password = self.entry_pswrd.get()

        print(
            'You got it!'
            if username == 'BigBoy' and int(password) == 55595
            else 'Boohoo, you are incorrect'
        )

if __name__ == '__main__':
    app = Application()
    app.mainloop()

A couple of quick suggestions when you overcome the issue above: you can perhaps slightly improve the user experience by providing a button to clear the entry boxes once the user's credentials are entered, or better yet - make it automatically clear those two fields. Also, password field could hide the characters you actually type.

A long-term-wise suggestion: you need to catch up on your OOP! :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM