使用类并获取 AttributeError: 'NoneType' object has no attribute 'configure'

问题描述 投票:0回答:0

嗨,我是编码新手,不断收到此错误 AttributeError: 'NoneType' object has no attribute 'configure' 我已经搜索了一段时间以弄清楚发生了什么,但我无法理解这个想法。我正在尝试使用

grab()
函数来更改原始框架的文本,但是当我这样做时它会返回错误。当代码中没有涉及类时,我理解这个问题,因为这些是我在 Internet 上看到的唯一示例。我用类创建了一个新的测试代码,并尝试首先调用类名,但它起作用了,我对我的原始代码做了同样的事情,但它不起作用。

import customtkinter
import random

bankbalancenumber = random.randint(10000,150000)
account_number = random.randint(1111111111, 999999999999)

login_list = []


class login_screen(customtkinter.CTk):
    def open_bank_account(self):
        self.MainScreen = MainScreen(self)

    def signup(self):
        global login
        login_list.append(self.entry_1.get())
        login_list.append(self.entry_2.get())
        print(login_list)

    def login(self):
        print(login_list)
        if login_list[0] == self.entry_1.get() and login_list[1] == self.entry_2.get():
            self.open_bank_account()
        else:
            print("wrong username or password")
    def __init__(self):
        super().__init__()
        customtkinter.set_appearance_mode("dark")
        customtkinter.set_default_color_theme("blue")
        self.geometry("250x300")  # 900x1080
        self.title("Login")

        self.columnconfigure(0, weight=1)

        self.label_1 = customtkinter.CTkLabel(self, text="Bank Account", justify=customtkinter.RIGHT)
        self.label_1.grid(row=0, column=0, pady=10, padx=10)

        self.button_1 = customtkinter.CTkButton(self, command=self.signup, text="signup")
        self.button_1.grid(row=5, column=0, pady=10, padx=10)

        self.button_2 = customtkinter.CTkButton(self, command=self.login, text="login")
        self.button_2.grid(row=4, column=0, pady=10, padx=10)

        self.entry_1 = customtkinter.CTkEntry(self, placeholder_text="Username")
        self.entry_1.grid(row=2, column=0, pady=10, padx=10)

        self.entry_2 = customtkinter.CTkEntry(self, placeholder_text="Password")
        self.entry_2.grid(row=3, column=0, pady=10, padx=10)


class MainScreen(customtkinter.CTkToplevel):
    bankbalance = None

    def open_deposit(self):
        self.deopsit_window = DepositWindow(self)

    def deposit(self):
        self.open_deposit()

    def __init__(self, fg_color=None, *args, **kwargs):
        super().__init__(*args, **kwargs)
        # Add widgets to the frame
        self.geometry("700x700")
        self.title("Main")

        self.bankbalance=customtkinter.CTkLabel(self, font=("Arial", 24), text="$" + str(bankbalancenumber))
        self.bankbalance.grid(row=0, column=0, rowspan=2, pady=10, padx=50, stick="W")

        self.columnconfigure(0, weight=1)
        self.columnconfigure(1, weight=1)
        self.columnconfigure(2, weight=1)
        self.rowconfigure(0, weight=1)
        self.rowconfigure(1, weight=0)
        self.rowconfigure(2, weight=1)
        self.rowconfigure(3, weight=1)
        self.rowconfigure(4, weight=1)
        self.rowconfigure(5, weight=1)
        self.rowconfigure(6, weight=1)
        self.rowconfigure(7, weight=1)

        self.label_3 =  customtkinter.CTkLabel(self, text=login_list[0] + "'s Account")
        self.label_3.grid(row=0, column=0, pady=10, padx=10, sticky="NW" )
        self.label_4 = customtkinter.CTkLabel(self, text="Account# " + str(account_number))
        self.label_4.grid(row=0, column=3, pady=10, padx=10, sticky="NE")
        self.button_3 = customtkinter.CTkButton(self, text="Deposit", command=self.deposit)
        self.button_3.grid(row=1, column=3, sticky="E", padx=50, pady=10)
        self.button_4 = customtkinter.CTkButton(self, text="Withdraw")
        self.button_4.grid(row=2, column=3, sticky="E", padx=50, pady=0)

class DepositWindow(customtkinter.CTkToplevel):
    def change(self):
        MainScreen.bankbalance.configure(text = str(bankbalancenumber))
    def grab(self):
        global bankbalancenumber
        moneyin = self.entry_3.get()
        MONEYIN = int(moneyin)
        bankbalancenumber += MONEYIN
        print(bankbalancenumber)
        self.change()
        return bankbalancenumber



    def __init__(self, fg_color=None, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.title("Deposit")
        self.geometry("150x250")

        self.button_5 = customtkinter.CTkButton(self, text="deposit", command=self.grab)
        self.button_5.grid(pady=10, padx=10, row=2)
        self.entry_3 = customtkinter.CTkEntry(self)
        self.entry_3.grid(row=1, pady=10, padx=10)



if __name__ == "__main__":
    app = login_screen()
    app.mainloop()
python attributeerror customtkinter
© www.soinside.com 2019 - 2024. All rights reserved.