我遇到了麻烦。我写了两个类,第一个是软件的主要gui,第二个是登录。现在,在登录窗口中我需要编写一个小的验证函数。我写了它,但我需要在验证此函数后调用主gui类并继承自Tk
。我知道我的代码非常差,但我需要帮助。
class main_gui(Frame):
def __init__(self,master, self.username, self.password):
self.path = os.getcwd()
self.master = master
self.username = StringVar()
self.password = StringVar()
master.title("main menu")
master.geometry("800x600")
master.config(background="white")
master.resizable(width="false", height="false")
master.iconbitmap(self.path + r"/img/icon.ico")
self.mmenu = Menu(master)
self.menubar = Menu(self.mmenu, tearoff=0)
self.menubar.add_command(label="about", command=aboutme)
self.menubar.add_command(label="Exit!", command=master.destroy)
self.mmenu.add_cascade(label="menu", menu=self.menubar)
master.config(menu=self.mmenu)
class login(main_gui):
def __init__(self):
self.master.title("login")
self.master.geometry("350x250")
self.master.config(background="white")
self.master.resizable(width="false", height="false")
self.master.iconbitmap(self.path + r"/img/icon.ico")
self.user_label = Label(master, text="user name")
self.user_label.pack()
self.user_value = Entry(master, textvariable=self.username)
self.user_value.pack()
self.password_label = Label(master, text="password")
self.password_label.pack()
self.password_value = Entry(master, textvariable=self.password)
self.password_value.pack()
self.login = Button(master, text="login" )
self.login.pack()
def validation(self):
if self.username.get() == "admin" :
main_gui()
root = Tk()
hm = main_gui(root)
root.mainloop()
答案 0 :(得分:0)
main_gui类的初始化方法并不完全正确。您需要将其更改为以下内容。
def __init__(self, master, username, password):
self.path = os.getcwd()
self.master = master
self.username = username
self.password = password
master.title("main menu")
master.geometry("800x600")
master.config(background="white")
master.resizable(width="false", height="false")
master.iconbitmap(self.path + r"/img/icon.ico")
self.mmenu = Menu(master)
self.menubar = Menu(self.mmenu ,tearoff=0)
self.menubar.add_command(label="about",command=aboutme)
self.menubar.add_command(label="Exit!", command=master.destroy)
self.mmenu.add_cascade(label="menu", menu=self.menubar)
master.config(menu=self.mmenu)
你看到我做了哪些改变? self
作为第一个参数传入函数,然后master
和username
在没有self.
的情况下传入。这样,您就可以将其值分配给代码正文中的self.master
和self.username
。
希望这对您有所帮助。
答案 1 :(得分:0)
我认为你的想法是错误的。您不需要继承main_gui
。这对我来说毫无意义。
看起来你有两个独立的窗户,你可以用两种方式。
第一:
第二:
Toplevel
创建Login作为子/子窗口/对话框(而非主窗口),这两种情况都不需要继承
第一个版本可能如下所示:
from tkinter import *
import sys
import os
class MainGui():
def __init__(self, username, password):
self.master = Tk()
self.path = os.getcwd()
self.path = os.path.realpath(sys.argv[0])
self.username = StringVar(value=username)
self.password = StringVar(value=password)
self.master.title("main menu")
self.master.geometry("800x600")
self.master.config(background="white")
self.master.resizable(width="false", height="false")
#master.iconbitmap(self.path + r"/img/icon.ico")
self.mmenu = Menu(self.master)
self.menubar = Menu(self.mmenu ,tearoff=0)
self.menubar.add_command(label="about",command=self.aboutme)
self.menubar.add_command(label="Exit!", command=self.master.destroy)
self.mmenu.add_cascade(label="menu", menu=self.menubar)
self.master.config(menu=self.mmenu)
self.master.mainloop()
def aboutme(self):
pass
class Login():
def __init__(self):
self.master = Tk()
self.path = os.path.realpath(sys.argv[0])
self.master.title("login")
self.master.geometry("350x250")
self.master.config(background="white")
self.master.resizable(width="false", height="false")
#self.master.iconbitmap(self.path + r"/img/icon.ico")
self.username = StringVar()
self.password = StringVar()
self.user_label = Label(self.master, text="user name")
self.user_label.pack()
self.user_value = Entry(self.master, textvariable=self.username)
self.user_value.pack()
self.password_label = Label(self.master, text="password")
self.password_label.pack()
self.password_value = Entry(self.master, textvariable=self.password)
self.password_value.pack()
self.login = Button(self.master, text="login", command=self.validation)
self.login.pack()
self.message = Label(self.master)
self.message.pack()
self.message.mainloop()
def validation(self):
if self.username.get() == "admin" :
self.master.destroy()
MainGui(self.username.get(), self.password)
else:
self.message['text'] = "Wrong !!!"
# --- start ---
Login()