この記事では、主に Python ログイン システムのログイン、登録、終了のためのプログラミング コードを紹介しますが、一定の参考値があり、必要な友人が参照することができます。この記事を読んで多くのことを理解していただければ幸いです。編集者と一緒に理解を深めましょう。
設計タスク
プログラムのログインインターフェイスを事前に設計し、設計手順を詳細に分析します。
プログラムの詳細な分析
基本的なフレームデザイン
「」
tkinterをtkとしてインポートします
tkinter.messageboxをインポートする
root = tk.Tk() # アプリケーションウィンドウの作成
root.title("ユーザーログインインターフェースの設計")
root.geometry("230x100")
# --------ファンクションブロックコードの開始------
--------ファンクションブロックコードの終わり------
root.mainloop()
ユーザーにメッセージを表示するラベルをデザインする
labelName = tk.Label(root, text='ユーザー名:', justify=tk.RIGHT, width=80)
labelPwd = tk.Label(root, text='ユーザーパスワード:', justify=tk.RIGHT, width=80)
デザイン入力ボックス
エントリ名 = tk.Entry(root, width=80, textvariable=varName)
entryPwd = tk.Entry(root, show='*', width=80, textvariable=varPwd)
デザインボタン
buttonOk = tk.Button(root, text='登录',lief=tk.RAISED, command=login)
buttonCancel = tk.Button(root, text='重置',lief=tk.RAISED, command=cancel)
buttonquit = tk.Button(root, text='退出',lief=tk.RAISED, command=_quit)
デザイン機能
**関連する変数**
varName = tk.StringVar()
varName.set('')
varPwd = tk.StringVar()
varPwd.set('')
**ログインボタン処理機能**
def ログイン():
# ユーザー名とパスワードを取得する
名前 = エントリ名.get()
pwd = エントリーPwd.get()
名前 == 'admin' および pwd == '123456' の場合:
tk.messagebox.showinfo(title='Python tkinter', message='OK')
それ以外:
tk.messagebox.showerror('Python tkinter', message='エラー')
**再入力ボタン処理機能**
デフォルトキャンセル():
# ユーザーが入力したユーザー名とパスワードをクリアします
varName.set('')
varPwd.set('')
**終了ボタン処理機能**
def _quit():
root.quit()
root.destroy()
各種コンポーネントの配置
ラベル名.place(x=10、y=5、幅=80、高さ=20)
labelPwd.place(x=10、y=30、幅=80、高さ=20)
エントリ名.place(x=100, y=5, width=80, height=20)
entryPwd.place(x=100、y=30、幅=80、高さ=20)
buttonOk.place(x=30, y=70, width=50, height=20)
buttonCancel.place(x=90, y=70, width=50, height=20)
buttonquit.place(x=150, y=70, width=50, height=20)
完全なプログラムの組み立て
tkinterをtkとしてインポートします
tkinter.messageboxをインポートする
root = tk.Tk() # アプリケーションウィンドウの作成
root.title("ユーザーログインインターフェースの設計")
root.geometry("230x100")
--------ファンクションブロックコードの開始------
機能的な機能デザイン
varName = tk.StringVar()
varName.set('')
varPwd = tk.StringVar()
varPwd.set('')
def ログイン():
# ユーザー名とパスワードを取得する
名前 = エントリ名.get()
pwd = エントリーPwd.get()
名前 == 'admin' および pwd == '123456' の場合:
tk.messagebox.showinfo(title='Python tkinter', message='OK')
それ以外:
tk.messagebox.showerror('Python tkinter', message='エラー')
デフォルトキャンセル():
# ユーザーが入力したユーザー名とパスワードをクリアします
varName.set('')
varPwd.set('')
def _quit():
root.quit()
root.destroy()
メインウィンドウの各種コンポーネントの設計
labelName = tk.Label(root, text='ユーザー名:', justify=tk.RIGHT, width=80)
labelPwd = tk.Label(root, text='ユーザーパスワード:', justify=tk.RIGHT, width=80)
エントリ名 = tk.Entry(root, width=80, textvariable=varName)
entryPwd = tk.Entry(root, show='*', width=80, textvariable=varPwd)
buttonOk = tk.Button(root, text='登录',lief=tk.RAISED, command=login)
buttonCancel = tk.Button(root, text='重置',lief=tk.RAISED, command=cancel)
buttonquit = tk.Button(root, text='退出',lief=tk.RAISED, command=_quit)
メインウィンドウにおける各コンポーネントの配置位置=編成
ラベル名.place(x=10、y=5、幅=80、高さ=20)
labelPwd.place(x=10、y=30、幅=80、高さ=20)
エントリ名.place(x=100, y=5, width=80, height=20)
entryPwd.place(x=100、y=30、幅=80、高さ=20)
buttonOk.place(x=30, y=70, width=50, height=20)
buttonCancel.place(x=90, y=70, width=50, height=20)
buttonquit.place(x=150, y=70, width=50, height=20)
--------ファンクションブロックコードの終わり------
root.mainloop() # ウィンドウ実行ループ
最終効果
![](https://img2018.cnblogs.com/blog/1372901/201810/1372901-20181021182129052-135871456.jpg)
![](https://img2018.cnblogs.com/blog/1372901/201810/1372901-20181021182133505-1654026029.jpg)
![](https://img2018.cnblogs.com/blog/1372901/201810/1372901-20181021182137374-229183115.jpg)