问题描述
写了两个窗口,一个为主界面窗口,一个为弹窗,主界面窗口与弹窗均绑定了esc键关闭窗口的键盘事件,当弹出窗口后因为键盘的焦点仍然在主窗口(应该是这个原因吧),所以按下esc键时会关闭主窗口,而不会关闭弹窗(当鼠标点击弹窗时esc可以使用),所以希望创建弹窗后可以在按下esc键时关闭弹窗而不是主窗口。
截图


部分代码
主窗口
#前面定义了一个叫main_root的主窗口
# 定义登录页面的函数
def signinpage(root_name):
global si_page
si_page = tk.Frame(root_name, height=rooth, width=rootw, bg=black)
# 定义鼠标悬浮在Button上时前景色变色函数
def sp_on_enter(e):
homepage_button['fg'] = red
def sp_on_leave(e):
homepage_button['fg'] = 'SystemButtonFace'
def sb_on_enter(e):
si_button['fg'] = red
def sb_on_leave(e):
si_button['fg'] = 'SystemButtonFace'
# 放置注册页面框架
si_page.pack()
# 定义返回主页按钮
homepage_button = tk.Button(si_page, text="返回主界面", font=(font2, 30), bg=black, fg=white, bd=0
, activeforeground=red, activebackground=black,
command=lambda: [signinpage_d(), judge_homepage(root_name)])
homepage_button.place(x=rootw - homepage_button.winfo_reqwidth() - 50, y=20)
# 响应鼠标悬浮在Button上时前景色变色函数
homepage_button.bind("<Enter>", sp_on_enter)
homepage_button.bind("<Leave>", sp_on_leave)
# 标题“SIGN IN”
signin_labal = tk.Label(si_page, text="登陆账号"
, font=(font1, 80), fg=red, bg=black)
signin_labal.place(x=50, y=20)
# 创建标签文本“账号”、“密码”
account_labal = tk.Label(si_page, height=1, width=10, text="账号:", font=(font2, int(rootw / 25)),
bg=black, fg=white, anchor="e")
password_labal = tk.Label(si_page, height=1, width=10, text="密码:", font=(font2, int(rootw / 25)),
bg=black, fg=white, anchor="e")
account_labal.place(x=100, y=230)
password_labal.place(x=100, y=430)
# 定义StringVar变量,设置及获取输入框内容
account = tk.StringVar()
password = tk.StringVar()
# 创建账号和密码的输入框
account_entry = tk.Entry(si_page, textvariable=account, font=(font2, 50), bg=black, fg=white,
bd=5, insertbackground=white)
password_entry = tk.Entry(si_page, textvariable=password, font=(font2, 50), bg=black, fg=white,
bd=5, insertbackground=white, show="*") # 密码框显示“*”
account_entry.place(x=120 + account_labal.winfo_reqwidth(), y=230)
password_entry.place(x=120 + password_labal.winfo_reqwidth(), y=430)
def sign_in_button_function():
try:
# 读取json数据
file = open(f"acc_data/{account.get()}.json", "a+") # 打开json文件
file.seek(0, 0) # 将文件光标移至开头
json_list = file.readlines() # 将json文件读取为list型,格式如[{}]
file.close() # 关闭json文件
json_dict = json.loads(json_list[0])
encryptstr = json_dict["password"]
decryptstr = ftw_encryptpwd.decrypt_str(encryptstr)
if password.get() == decryptstr:
fm.ftw_messagebox("提示", "登录成功!", 30)
else:
fm.ftw_messagebox("提示", "用户名或密码错误!", 30)
except:
fm.ftw_messagebox("提示", "用户名或密码错误!", 30)
# 定义登录按钮
si_button = tk.Button(si_page, text="登录!", font=(font2, 45), bg=black, fg=white, bd=0
, activeforeground=red, activebackground=black,
command=sign_in_button_function)
si_button.place(x=(rootw - si_button.winfo_reqwidth()) / 2, y=650)
# 响应鼠标悬浮在Button上时前景色变色函数
si_button.bind("<Enter>", sb_on_enter)
si_button.bind("<Leave>", sb_on_leave)
# 定义ESC键退出函数
def key_escape(event):
main_root.destroy()
main_root.bind("<Escape>", key_escape)
弹窗
def ftw_messagebox(title: str, message: str, size: int):
"""
该函数用于生成一个消息弹窗,可编辑其标题(title)、消息内容(message)、文字尺寸(size)
:param title: 弹窗窗口所展示的标题,类型为字符串
:param message: 想要在消息弹窗中展示的文字内容,类型为字符串
:param size: :message的文字大小
:return: 返回一个标题为title,标题字体为“Aa耕闲书”,内容为message,内容字体为”Aa后浪行楷“,背景为black, 字体大小为size的弹窗
"""
blackscreen = tk.Tk()
screenw = blackscreen.winfo_screenwidth()
screenh = blackscreen.winfo_screenheight()
rootw = screenw
rooth = screenh
w = 0
h = 0
blackscreen.overrideredirect(True)
blackscreen.geometry(f"{rootw}x{rooth}+{w}+{h}")
blackscreen.title(title)
blackscreen.wm_attributes('-alpha', 0.5)
blackscreen["bg"] = black
blackscreen.iconbitmap("res/ftw_icon.ico")
ftwm = tk.Tk()
screenw = ftwm.winfo_screenwidth()
screenh = ftwm.winfo_screenheight()
rootw = 500
rooth = 300
w = int((screenw - rootw) / 2)
h = int((screenh - rooth) / 2)
ftwm.overrideredirect(True)
ftwm.geometry(f"{rootw}x{rooth}+{w}+{h}")
ftwm.title(title)
ftwm.wm_attributes('-topmost', 1)
ftwm["bg"] = white
ftwm.iconbitmap("res/ftw_icon.ico")
def q():
ftwm.destroy()
blackscreen.destroy()
# 定义鼠标悬浮在Button上时前景色变色函数
def t_on_enter(e):
titlebutton['fg'] = title_red
def t_on_leave(e):
titlebutton['fg'] = red
fram = tk.Frame(ftwm, height=rooth - 2, width=rootw - 2, bg=black)
titlelabel = tk.Label(fram, bg=black, fg=red, text=title, font=(font1, 40))
titlelabel.place(x=5, y=0)
titlebutton = tk.Button(fram, bg=black, bd=0, fg=red, text="X", font=(title_font, 30), command=q,
activeforeground=white, activebackground=black)
titlebutton.place(x=rootw - titlebutton.winfo_reqwidth() - 5, y=-7)
# 响应鼠标悬浮在Button上时前景色变色函数
titlebutton.bind("<Enter>", t_on_enter)
titlebutton.bind("<Leave>", t_on_leave)
tk.Label(fram, bg=white, fg=white, font=(font1, 1), height=1, width=492).place(x=0, y=55)
tk.Label(fram, bg=black, fg=black, font=(font1, 1), height=1, width=492).place(x=0, y=56)
msglabel = tk.Label(fram, bg=black, fg=white, text=message, font=(font3, size))
msglabel.place(x=int((rootw - msglabel.winfo_reqwidth()) / 2), y=int((rooth - msglabel.winfo_reqheight()) / 2 + 20))
fram.place(x=int((rootw - fram.winfo_reqwidth()) / 2), y=int((rooth - fram.winfo_reqheight()) / 2))
# 定义ESC键退出函数
def key_escape(event):
blackscreen.destroy()
ftwm.destroy()
ftwm.bind("<Escape>", key_escape)
blackscreen.mainloop()
return ftwm