我的目的是在第一个frame完成操作后销毁本Frame,然后设置一个全局变量,后面通过对这个全局变量的判断生成不同的Frame,然而现在操作还没运行完,后面进行判断的代码就执行了。全都在同一个窗口上进行。
interface.run()这个不会阻塞吗,为什么这个run()没运行完就执行后面的判断打印了,
现在发现是but.bind("", self.__change)这里触发之后就一直阻塞在这里,不向后面执行
if self.cmd==1:
print('manager')
elif self.cmd==2:
print('staff')
else:
pass
fhttps://img-mid.csdnimg.cn/release/static/image/mid/ask/273653163086178.png "#left")
rom tkinter.ttk import *
from tkinter import Tk, StringVar, messagebox
import openpyxl as op
# __内部函数
# #_交互函数
# 读写账号
class Txt:
def __init__(self, path):
self.path = path
self.count = open(self.path, 'r+', encoding='utf8')
def read(self):
res = self.count.readlines()
count = {}
for i in res:
data = i.strip().split(':')
count[data[0]] = data[1]
return count
class Excel:
def __int__(self):
pass
class LoginInterface(Frame):
def __init__(self, parent=None):
super().__init__(parent)
self.count_manager_dict = count_manager_dict
self.pack(expand=1, fill="both")
self.frame = Frame(self)
self.frame.pack(side='left', expand=0, fill="x", padx=5, pady=5)
def __change(self, event):
text = event.widget['text']
if text == '管理员登录':
text = 'manager'
else:
text = 'staff'
# 根据text属性,获取信息,调用处理方法
data = {'user_name': self.name_str.get(), 'user_pwd': self.pwd_str.get()}
fun = getattr(self, '_%s_login' % text)
if fun(data):
# 登陆成功,销毁页面
self.frame.destroy()
print('销毁')
else:
pass
def _manager_login(self, data):
user_name = data['user_name']
user_pwd = data['user_pwd']
if user_name in self.count_manager_dict:
if user_pwd == self.count_manager_dict[user_name]:
global cmd
cmd = 1
messagebox.showinfo(title='成功', message='登陆成功')
return True
else:
messagebox.showerror(title='错误', message='密码错误')
return False
else:
messagebox.showerror(title='错误', message='账号不存在')
return False
def __get_view(self):
self.name_str = StringVar()
self.name_str.set('请输入账号')
name_ent = Entry(master=self.frame, textvariable=self.name_str)
name_ent.grid(row=1, columnspan=2, column=1, pady=10)
self.pwd_str = StringVar()
self.pwd_str.set('请输入密码')
pwd_ent = Entry(master=self.frame, textvariable=self.pwd_str)
pwd_ent.grid(row=2, columnspan=2, column=1, pady=10)
list_type = ['管理员登录', '员工登录']
for i in range(len(list_type)):
but = Button(self.frame, text=list_type[i])
but.grid(row=3, column=1 + i)
# 根据鼠标左键单击事件,切换页面
but.bind("<Button-1>", self.__change)
def run(self):
print('开始')
# 初始化界面并登录
self.__get_view()
print('结束')
# 管理员类
class Manager(Frame):
def __init__(self, parent=None):
super().__init__(parent)
self.pack()
self.frame = Frame(self)
self.frame.pack(expand=1, fill='both')
# 全局变量
wb = '员工考勤记录.xlsx'
ws = '考情记录'
count_path = r'账号.txt'
cmd = 0
if __name__ == '__main__':
count = Txt(count_path)
count_manager_dict = count.read()
root = Tk()
root.title('员工考勤管理')
# root.state('zoomed')
# 登录部分
interface = LoginInterface(root)
interface.run()
print(cmd)
if cmd == 1:
print(cmd)
if cmd == 2:
print(cmd)
print(cmd)
root.mainloop()