-1
に失われた(コードのモジュラーを作ってみました)。次のようにメインのGUIのコードは、今Tkinterのイベントハンドラの参照は、私が区切りファイル<code>verification.py</code>にクラス<code>Verification</code>にイベントハンドラを置くウィジェット
from tkinter import *
from make_widget import *
from verification import *
class LoginFrame(Frame):
def __init__(self, parent):
super(LoginFrame, self).__init__()
self.parent = parent
self.initUI()
# initialize the login screen UI
def initUI(self):
# Set up login frame properties
self.parent.title("Login Screen")
# creating instruction label
self.inst_lbl = MakeWidget.make_label(self.parent, "Please login to continue")
# creating labels and entries for user name and password
self.user_name = MakeWidget.make_entry(self.parent, caption="User Name:")
self.pwd = MakeWidget.make_entry(self.parent, caption="User Password:", show="*")
# create a login button
login_btn = MakeWidget.make_button(self.parent, Verification.verify_user(self.user_name, self.pwd, self.inst_lbl), "Login")
def main():
top = Tk()
app = LoginFrame(top)
top.mainloop()
if __name__ == '__main__':
main()
# verification.py
from tkinter import *
class Verification:
# verify user name and password
#----------------------------------------------------------------------
@staticmethod
def verify_user(user_name, pwd, inst_lbl):
"""verify users"""
if user_name.get() == "admin" and pwd.get() == "123":
inst_lbl.configure(text="User verified")
else:
inst_lbl.configure(text="Access denied. Invalid username or password")
# make_widget.py
from tkinter import *
class MakeWidget(Frame):
def __init__(self, parent):
super(MakeWidget, self).__init__()
self.parent = parent
# create a button widget
#----------------------------------------------------------------------
@staticmethod
def make_button(parent, command, caption=NONE, side=TOP, width=0, **options):
"""make a button"""
btn = Button(parent, text=caption, command=command)
if side is not TOP:
btn.pack(side=side)
else:
btn.pack()
return btn
# create a label widget
@staticmethod
def make_label(parent, caption=NONE, side=TOP, **options):
label = Label(parent, text=caption, **options)
if side is not TOP:
label.pack(side=side)
else:
label.pack()
return label
# create a entry widget
@staticmethod
def make_entry(parent, caption=NONE, side=TOP, width=0, **options):
MakeWidget.make_label(parent, caption, side)
entry = Entry(parent, **options)
if width:
entry.config(width=width)
if side is not TOP:
entry.pack(side=side)
else:
entry.pack()
return entry
ですが、私はLoginFrame
でinst_lbl
がconfigure
のことができるようにとuser_name
とpwd
に基づいて新しいテキストを表示することが期待、しかしinst_lbl
はありません(テキストを変更しませんでしたエラーが発生しました)。それでは、どのような問題を解決します。あなたはすぐがVerification.verify_user
を呼び出し、ボタンコマンドので、結果を代入している
login_btn = MakeWidget.make_button(self.parent, Verification.verify_user(self.user_name, self.pwd, self.inst_lbl), "Login")
:
"起こらなかった"とはどういう意味ですか?エラーが発生しましたか?もしそうなら、どんなエラー?また、このコードはインデントエラーがあり、make_labelためのコードが欠落している、実行されません。私はOPを変更した@BryanOakley –
が、それは少し明確にし、 – daiyue
インデントはまだ台無しにされたすべてのコードを追加しました。そして、だけではなく、一つのファイルにクラスのすべてを入れて、三つのファイルにコードを保存するために私たちを強制します。 –