我們現在的問題是,如何在登錄界面點了確定之后可以進入下一個界面,從圖1到圖2
圖1
圖2
下面是代碼
import tkinter as tk # 使用Tkinter前需要先匯入
import tkinter.messagebox
import pickle
# 第1步,實體化object,建立視窗window
window = tk.Tk()
# 第2步,給視窗的可視化起名字
window.title('Welcome to Hongwei Website')
# 第3步,設定視窗的大小(長 * 寬)
window.geometry('400x300') # 這里的乘是小x
# 第4步,加載 wellcome image
canvas = tk.Canvas(window, width=400, height=135, bg='green')
canvas.pack(side='top')
tk.Label(window, text='Welcome', font=('Arial', 16)).pack()
# 第5步,用戶資訊
tk.Label(window, text='User name:', font=('Arial', 14)).place(x=10, y=170)
tk.Label(window, text='Password:', font=('Arial', 14)).place(x=10, y=210)
# 第6步,用戶登錄輸入框entry
# 用戶名
var_usr_name = tk.StringVar()
var_usr_name.set('[email protected]')
entry_usr_name = tk.Entry(window, textvariable=var_usr_name, font=('Arial', 14))
entry_usr_name.place(x=120, y=175)
# 用戶密碼
var_usr_pwd = tk.StringVar()
entry_usr_pwd = tk.Entry(window, textvariable=var_usr_pwd, font=('Arial', 14), show='*')
entry_usr_pwd.place(x=120, y=215)
# 第8步,定義用戶登錄功能
def usr_login():
# 這兩行代碼就是獲取用戶輸入的usr_name和usr_pwd
usr_name = var_usr_name.get()
usr_pwd = var_usr_pwd.get()
# 這里設定例外捕獲,當我們第一次訪問用戶資訊檔案時是不存在的,所以這里設定例外捕獲。
# 中間的兩行就是我們的匹配,即程式將輸入的資訊和檔案中的資訊匹配。
try:
with open('usrs_info.pickle', 'rb') as usr_file:
usrs_info = pickle.load(usr_file)
except FileNotFoundError:
# 這里就是我們在沒有讀取到`usr_file`的時候,程式會創建一個`usr_file`這個檔案,并將管理員
# 的用戶和密碼寫入,即用戶名為`admin`密碼為`admin`。
with open('usrs_info.pickle', 'wb') as usr_file:
usrs_info = {'admin': 'admin'}
pickle.dump(usrs_info, usr_file)
usr_file.close() # 必須先關閉,否則pickle.load()會出現EOFError: Ran out of input
# 如果用戶名和密碼與檔案中的匹配成功,則會登錄成功,并跳出彈窗how are you? 加上你的用戶名。
if usr_name in usrs_info:
if usr_pwd == usrs_info[usr_name]:
tkinter.messagebox.showinfo(title='Welcome', message='How are you? ' + usr_name)
# 如果用戶名匹配成功,而密碼輸入錯誤,則會彈出'Error, your password is wrong, try again.'
else:
tkinter.messagebox.showerror(message='Error, your password is wrong, try again.')
else: # 如果發現用戶名不存在
is_sign_up = tkinter.messagebox.askyesno('Welcome! ', 'You have not sign up yet. Sign up now?')
# 提示需不需要注冊新用戶
if is_sign_up:
usr_sign_up()
# 第9步,定義用戶注冊功能
def usr_sign_up():
def sign_to_Hongwei_Website():
# 以下三行就是獲取我們注冊時所輸入的資訊
np = new_pwd.get()
npf = new_pwd_confirm.get()
nn = new_name.get()
# 這里是打開我們記錄資料的檔案,將注冊資訊讀出
with open('usrs_info.pickle', 'rb') as usr_file:
exist_usr_info = pickle.load(usr_file)
# 這里就是判斷,如果兩次密碼輸入不一致,則提示Error, Password and confirm password must be the same!
if np != npf:
tkinter.messagebox.showerror('Error', 'Password and confirm password must be the same!')
# 如果用戶名已經在我們的資料檔案中,則提示Error, The user has already signed up!
elif nn in exist_usr_info:
tkinter.messagebox.showerror('Error', 'The user has already signed up!')
# 最后如果輸入無以上錯誤,則將注冊輸入的資訊記錄到檔案當中,并提示注冊成功Welcome!,You have successfully signed up!,然后銷毀視窗。
else:
exist_usr_info[nn] = np
with open('usrs_info.pickle', 'wb') as usr_file:
pickle.dump(exist_usr_info, usr_file)
tkinter.messagebox.showinfo('Welcome', 'You have successfully signed up!')
# 然后銷毀視窗。
window_sign_up.destroy()
# 定義長在視窗上的視窗
window_sign_up = tk.Toplevel(window)
window_sign_up.geometry('300x200')
window_sign_up.title('Sign up window')
new_name = tk.StringVar() # 將輸入的注冊名賦值給變數
new_name.set('[email protected]') # 將最初顯示定為'[email protected]'
tk.Label(window_sign_up, text='User name: ').place(x=10, y=10) # 將`User name:`放置在坐標(10,10)。
entry_new_name = tk.Entry(window_sign_up, textvariable=new_name) # 創建一個注冊名的`entry`,變數為`new_name`
entry_new_name.place(x=130, y=10) # `entry`放置在坐標(150,10).
new_pwd = tk.StringVar()
tk.Label(window_sign_up, text='Password: ').place(x=10, y=50)
entry_usr_pwd = tk.Entry(window_sign_up, textvariable=new_pwd, show='*')
entry_usr_pwd.place(x=130, y=50)
new_pwd_confirm = tk.StringVar()
tk.Label(window_sign_up, text='Confirm password: ').place(x=10, y=90)
entry_usr_pwd_confirm = tk.Entry(window_sign_up, textvariable=new_pwd_confirm, show='*')
entry_usr_pwd_confirm.place(x=130, y=90)
# 下面的 sign_to_Hongwei_Website
btn_comfirm_sign_up = tk.Button(window_sign_up, text='Sign up', command=sign_to_Hongwei_Website)
btn_comfirm_sign_up.place(x=180, y=120)
# 第7步,login and sign up 按鈕
btn_login = tk.Button(window, text='Login', command=usr_login)
btn_login.place(x=120, y=240)
btn_sign_up = tk.Button(window, text='Sign up', command=usr_sign_up)
btn_sign_up.place(x=200, y=240)
# 第10步,主視窗回圈顯示
window.mainloop()
from tkinter import *
from tkinter.messagebox import showinfo
root = Tk()
def create3():
top2 = Toplevel()
top2.title('Python')
Button(top2, text='植物一',command=reply).grid(row=1, column=1, padx=1, pady=1)
Button(top2, text='植物二',command=reply).grid(row=1, column=2, padx=1, pady=1)
Button(top2, text='植物三',command=reply).grid(row=1, column=3, padx=1, pady=1)
def reply():
showinfo(title='新視窗', message='恭喜兌換成功')
def create():
top2 = Toplevel()
top2.title('Python')
Button(top2, text='瓶數').grid(row=1, column=1, padx=1, pady=1)
def create2():
top1 = Toplevel()
top1.title('Python')
Button(top1, text='兌換',command=create).grid(row=1, column=1, padx=1, pady=1)
Button(root, text='兌換', command=create3).pack()
Button(root, text='投瓶', command=create).pack()
mainloop()
謝謝!

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/122795.html
上一篇:Python turtle
