我查看了不同的問題并查看了不同的資源來解決我的問題,但我并不幸運,因為我對 Python 還很陌生。我試圖在最后運行我的程式
if __name__ == "__main__":
check = cursor.execute("SELECT * FROM masterpassword")
# if there is already a present password
if check.fetchall():
app = login()
else:
app = changeMaster()
app.mainloop()
但我收到一條錯誤訊息:
TypeError: changeMaster.__init__() missing 1 required positional argument: 'parent'
# change master password with correct input
class changeMaster(Toplevel):
def __init__(self, parent):
super().__init__(parent)
# display config
self.geometry("400x400")
self.title("change master password")
# main menu button
back = Button(self, text="main menu", command=self.destroy).grid(padx=20, pady=30)
# enter new password
Label(self, text="enter new password").place(x=250, y=300)
self.newPass = Entry(width = 20)
self.newPass.place(x=400, y=300)
# re-enter password
Label(self, text="re-enter password").place(x=250, y=400)
self.enter = Entry(width = 20)
self.enter.place(x=400, y=400)
# submit password
Button(self, text="submit", command=self.confirm).place(x=350, y=400)
def confirm(self):
# if the passwords match
if self.newPass.get() == self.enter.get():
# new password is set to hashedPassword
hashedPassword = self.newPass.get()
# inserts the new password into the database
insert_password = """INSERT INTO masterpassword(password)
VALUES(?)"""
#
cursor.execute(insert_password, [(hashedPassword)])
# saves into database
db.commit()
# switch to the viewpassword window
window = viewPass(self)
window.grab_set()
else:
Label(self, text="passwords do not match").place(x=375, y=500)
任何反饋將不勝感激!
uj5u.com熱心網友回復:
Toplevel用于創建第二個視窗,它需要主視窗作為父視窗。
因此,首先您必須創建主視窗,然后將其用作父視窗
root = Tk()
app = changeMaster(root)
root.mainloop()
如果它必須是那么只有 window 那么你應該使用Tk(without parent) 而不是Toplevel
class changeMaster(Tk):
def __init__(self):
super().__init__()
# ... code ...
app = changeMaster()
app.mainloop()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/504278.html
