简体   繁体   English

GUI Python 登录错误:缺少参数

[英]GUI Python Login Error: Missing an Argument

I am having an issue with this GUI login I am trying to make.我在尝试进行此 GUI 登录时遇到问题。 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.由于您使用 OOP 作为开发用户界面的范例,您应该充分利用它提供的所有好处。 However, something tells me you're not quite familiar with classes, objects, instance variables and other OOP concepts.但是,有些事情告诉我您不太熟悉类、对象、实例变量和其他 OOP 概念。

  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.我建议您将您的类设计为实际继承自tkinter.Tk ,因为用户界面部分(小部件)的状态和行为被分组到单独的对象中,这些对象依次位于单独的命名空间中。

  2. Apart from that, I also suggest you get rid of from tkinter import * , since you don't know what names that imports.除此之外,我还建议您摆脱from tkinter import * ,因为您不知道导入的名称。 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.改用import tkinter as tk ,这是 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:您可能想阅读 PEP8 在命名约定关键字参数周围的空格以及 Python 编码风格指南方面的内容:

  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!一个长期明智的建议:你需要赶上你的 OOP! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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