马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
我尝试了from tkinter import *
master = Tk()
v = StringVar()
def tes(content, reason, name):
if content == "小甲鱼":
print("正确!")
print(content, reason, name)
return True
else:
print("错误!")
print(content, reason, name)
return False
tesCMD = master.register(tes)
e1 = Entry(master,
textvariable=v,
validate="focusout",
validatecommand=(tesCMD, '%P','1','2'))
# %P表示 当输入框的值允许改变,该值有效。该值为当前文本框内容
# %v(小写大写不一样的),当前validate的值
# %W表示该组件的名字
e2 = Entry(master)
e1.pack(padx=10, pady=10)
e2.pack(padx=10, pady=10)
mainloop()
可以运行
然后想在自己的代码中使用这个功能获取Entry内容并存入Excel,但不知道为什么存入excel的是字符串'%P'
下面是我的代码: def find_loc(enter):
global wbname, locpoint
subjects = pd.read_excel("C:\\Users\\86136\\.spyder-py3\\被试信息管理\\被试信息管理\\subject_load.xlsx")
subject_id = subjects.values[-1][0]
wbname = str(subject_id) + '.xlsx'
subjects = pd.read_excel(
"C:\\Users\\86136\\.spyder-py3\\被试信息管理\\被试信息管理\database\\{}".format(wbname))
titleline = list(subjects.keys())
locpoint = titleline.index(enter)
def data_to_excel(data,titlename):
find_loc(titlename)
wb = openpyxl.load_workbook(
"C:\\Users\\86136\\.spyder-py3\\被试信息管理\\被试信息管理\\database\\{}".format(wbname))
ws = wb['basic_info']
ws.cell(row=2, column=locpoint + 1).value = data
wb.save("C:\\Users\\86136\\.spyder-py3\\被试信息管理\\被试信息管理\\database\\{}".format(wbname))
wb.close()
def add_D():
if self.f1:
self.f1.destroy()
self.f1 = tk.Frame(self.canvas)
self.myscrollbar=tk.Scrollbar(self.myframe,orient="vertical",command=self.canvas.yview)
self.canvas.configure(yscrollcommand=self.myscrollbar.set)
self.myscrollbar.pack(side="right",fill="y")
self.canvas.pack(side="left")
self.canvas.create_window((0,0),window=self.f1,anchor='nw')
self.f1.bind("<Configure>",self.myfunction)
with open("C:\\Users\\86136\\.spyder-py3\\被试信息管理\\被试信息管理\\UKbiobank题库\\D.txt", 'r', encoding='utf-8') as Dtxt: # 只读方式打开编码为utf-8的文本文件
number = 0 # 文本文件中的行号,即
line = Dtxt.readlines() # 以读取一行为列表方法读取全部行,line为分解好的列表内容
Q_num = len(line)
rownum = 0
for x in range(Q_num):
str1 = line[x].split('*')
id = str(number+1)+". " # 题目序号
self.la1 = tk.Label(self.f1, text=str1[0]+', '+str1[1]+', '+str1[2], justify="left", height=2,
wraplength=500) # 题目,靠左,多行左对齐
self.la1.pack()
op_num = len(str1)-3
d2e = self.f1.register(data_to_excel)
self.var = tk.IntVar()
v = tk.StringVar()
for i in range(0,op_num):
if '___' in str1[i]:
tk.Label(self.f1,text=str1[3]).pack()
self.ra1_entry = tk.Entry(self.f1, textvariable=v, validate="focusout", validatecommand=(d2e,'%P',str1[0]))
self.ra1_entry.pack()
else:
self.ra1 = tk.Radiobutton(self.f1, text=str(i+1)+', ' + str1[i+3], variable=self.var,
value=i+1, justify="left", wraplength=430, command=(d2e,'%P',str1[0]))
self.ra1.pack()
rownum += op_num+1
self.thank=tk.Label(self.f1,text='请点击右下角的确认保存,然后点击左下角的菜单返回').pack()
self.btn1 = tk.Button(self.f1, text='菜单', command=menu)
self.btn1.pack(side='left')
|