Tkinter的问题
部分代码如下:def add_sutdent():
new_root = Tk()
new_root.title('学生注册')
new_root.geometry('300x300')
new_stu = Label(new_root, text='姓名>>>', font=('Heiti', 12)).grid(row=1, column=1)
stu = StringVar()
new_stu_entry = Entry(new_root, textvariable=stu, width=20).grid(row=1, column=2)
new_pho = Label(new_root, text='电话>>>', font=('Heiti', 12)).grid(row=2, column=1)
pho = StringVar()
new_pho_entry = Entry(new_root, textvariable=pho, font=('Heiti', 12), width=20).grid(row=2, column=2)
new_sex_choice = Label(new_root, text='性别>>>', font=('Heiti', 12)).grid(row=3, column=1)
sex = StringVar()
Radiobutton(new_root, text='male', variable=sex, value='male').grid(row=3, column=2, sticky='w')
Radiobutton(new_root, text='female', variable=sex, value='female').grid(row=3, column=2, sticky='e')
new_hob_choice = Label(new_root, text='爱好>>>', font=('Heiti', 12)).grid(row=4, column=1)
hob_1 = IntVar()
hob_2 = IntVar()
Checkbutton(new_root, text='唱', variable=hob_1, onvalue=1, offvalue=0).grid(row=4, column=2, sticky='w')
Checkbutton(new_root, text='跳', variable=hob_2, onvalue=1, offvalue=0).grid(row=4, column=2, sticky='e')
new_pov_choice = Label(new_root, text='省份>>>', font=('Heiti', 12)).grid(row=5, column=1)
pros = ['北京', '上海', '广州', '深圳']
pro = StringVar()
pro_choice = Combobox(new_root, textvariable=pro, width=15, values=pros, state='readonly').grid(row=5, column=2)
def get_stu_info():
stu_name = stu.get()
stu_pho = pho.get()
stu_sex = sex.get()
stu_pro = pro.get()
stu_like1 = hob_1.get()
stu_like2 = hob_2.get()
print(stu_name, stu_pho, stu_sex, stu_pro, stu_like1, stu_like2)
button_4 = Button(new_root, text='提交', width=12, command=get_stu_info).grid(row=6, columnspan=3, pady=20)
问题是这样的,这个函数单独拿出来使用的话是正常的返回值,能打印出学生属性,但是放在项目里,返回的总是两个0,也就是所有的值都获取不到,这是什么问题呢? 单独拿出来测试的话是没有问题的 from tkinter import *
from tkinter import Tk,messagebox,Toplevel
from tkinter.ttk import Combobox
import openpyxl
root= Tk()
root.title('登录界面')
root.geometry('300x200')
root.resizable(width=False,height=False)
#账号输入框
user_name = Label(root,text='账号>>>',font=('Heiti',12))
user_name.place(x=8, y=30)
user_name_text = StringVar()
user_name_entry = Entry(root,textvariable=user_name_text,font=('Heiti',12),width=20)
user_name_entry.place(x=60,y=30)
# 密码输入框
user_pwd = Label(root,text='密码>>>',font=('Heiti',12))
user_pwd.place(x=8,y=60)
user_pwd_text =StringVar()
user_pwd_entry = Entry(root,textvariable=user_pwd_text,font=('Heiti',12),width=20)
user_pwd_entry.place(x=60,y=60)
def data():
with open('./data.txt','r',encoding='utf-8') as f:
user_list = f.readlines()
user_dict = {}
for each_user in user_list:
user = each_user.strip().split(':')
pwd = each_user.strip().split(':')
user_dict=pwd
return user_dict
def get_input():
user_input = user_name_text.get()
pwd_input = user_pwd_text.get()
user_dict = data()
if user_input != '' and pwd_input != '':
if user_input in user_dict.keys():
if pwd_input == user_dict:
messagebox.showinfo(title='ok',message='欢迎回来' + user_input)
root.destroy()
else:
messagebox.showwarning(title='error',message='密码错误')
else:
messagebox.showerror(title='error',message='空用户,不存在')
else:
messagebox.showerror(title='error',message='账号,密码不能为空')
# print('不能为空')
def pop_win():
top = Toplevel()
top.title('注册')
top.geometry('300x200')
top.resizable(width=False,height=False)
Label(top,text='账号>>>').grid(row=1, column=2)
new_name = StringVar()
Entry(top,text=new_name).grid(row=1,column=4)
Label(top,text='密码>>>').grid(row=3,column=2)
new_pwd = StringVar()
Entry(top,text=new_pwd).grid(row=3,column=4)
Label(top, text='确认密码>>>').grid(row=5, column=2)
new_check = StringVar()
Entry(top, text=new_check).grid(row=5, column=4)
def check_pwd():
use_1 = new_name.get()
pwd_1 = new_pwd.get()
pwd_2 = new_check.get()
if use_1 == '' or pwd_1 == '' or pwd_2 == '' :
messagebox.showerror(title='error', message='Not null')
else:
if pwd_1 == pwd_2:
with open('./data.txt','a',encoding='utf-8') as fp:
fp.writelines('\n' + use_1 +':'+ pwd_1)
fp.flush()
messagebox.showinfo(title='ok', message='注册成功')
top.destroy()
else:
messagebox.showerror(title='error',message='两次密码不一致')
button_3 = Button(top,text='提交',width=15,command=check_pwd).grid(row=9,column=4)
def save_stu():
wb = openpyxl.Workbook()
sheet = wb.active
sheet['A1'] = '姓名'
sheet['B1'] = '电话'
sheet['C1'] = '性别'
sheet['D1'] = '省份'
sheet['E1'] = '爱好1'
sheet['F1'] = '爱好2'
wb.save('./学生信息.xlsx')
def add_sutdent():
new_root = Tk()
new_root.title('学生注册')
new_root.geometry('300x300')
new_stu = Label(new_root, text='姓名>>>', font=('Heiti', 12)).grid(row=1, column=1)
stu = StringVar()
new_stu_entry = Entry(new_root, textvariable=stu, width=20).grid(row=1, column=2)
new_pho = Label(new_root, text='电话>>>', font=('Heiti', 12)).grid(row=2, column=1)
pho = StringVar()
new_pho_entry = Entry(new_root, textvariable=pho, font=('Heiti', 12), width=20).grid(row=2, column=2)
new_sex_choice = Label(new_root, text='性别>>>', font=('Heiti', 12)).grid(row=3, column=1)
sex = StringVar()
Radiobutton(new_root, text='male', variable=sex, value='male').grid(row=3, column=2, sticky='w')
Radiobutton(new_root, text='female', variable=sex, value='female').grid(row=3, column=2, sticky='e')
new_hob_choice = Label(new_root, text='爱好>>>', font=('Heiti', 12)).grid(row=4, column=1)
hob_1 = IntVar()
hob_2 = IntVar()
Checkbutton(new_root, text='唱', variable=hob_1, onvalue=1, offvalue=0).grid(row=4, column=2, sticky='w')
Checkbutton(new_root, text='跳', variable=hob_2, onvalue=1, offvalue=0).grid(row=4, column=2, sticky='e')
new_pov_choice = Label(new_root, text='省份>>>', font=('Heiti', 12)).grid(row=5, column=1)
pros = ['北京', '上海', '广州', '深圳']
pro = StringVar()
pro_choice = Combobox(new_root, textvariable=pro, width=15, values=pros, state='readonly').grid(row=5, column=2)
def get_stu_info():
stu_name = stu.get()
stu_pho = pho.get()
stu_sex = sex.get()
stu_pro = pro.get()
stu_like1 = hob_1.get()
stu_like2 = hob_2.get()
print(stu_name, stu_pho, stu_sex, stu_pro, stu_like1, stu_like2)
button_4 = Button(new_root, text='提交', width=12, command=get_stu_info).grid(row=6, columnspan=3, pady=20)
#菜单栏
menu_bar = Menu(root)
stu_men = Menu(menu_bar)
#添加选项
stu_men.add_command(label='注册',command=add_sutdent)
stu_men.add_command(label='查询',command='')
#将菜单添加到菜单栏中
menu_bar.add_cascade(label='学生',menu=stu_men)
# 配置菜单栏
root.config(menu=menu_bar)
score_menu = Menu(menu_bar)
score_menu.add_command(label='添加',command='')
score_menu.add_command(label='查询',command='')
menu_bar.add_cascade(label='成绩',menu=score_menu)
root.config(menu=menu_bar)
button_1 = Button(root,text='Login in',font=('Heiti',12),width=8,command=get_input).place(x=40,y=100)
button_2 = Button(root,text='注册',font=('Heiti',12),width=8,command=pop_win).place(x=160,y=100)
root.mainloop()
# new_root.mainloop()
这是全部代码
页:
[1]