Tkinter-如何通过一个按钮从多个输入框获取数据值?
我在练习做一个小的图形用户界面(GUI),想请教一下大家的意见。
我正在尝试制作一个用于弯矩计算的界面,未来可能会扩展功能...我希望能找到一种方法,通过一个按钮来获取所有输入框的数据。现在我可以为每个输入框设置一个单独的按钮,这样也能工作,但我更希望能有一个简单的“提交”按钮...
如果有人能提供帮助或建议,我会非常感激,感谢!
当前代码:
import Tkinter as tk
class beam(tk.Tk):
def __init__(self,parent):
tk.Tk.__init__(self,parent)
self.parent = parent
self.initUI()
def initUI(self):
self.grid()
self.ent1 = tk.Entry(self)
self.ent1.grid(row=10,column=2)
self.ent2 = tk.Entry(self)
self.ent2.grid(row=12,column=2)
self.ent3 = tk.Entry(self)
self.ent3.grid(row=14,column=2)
self.ent4 = tk.Entry(self)
self.ent4.grid(row=16,column=2)
self.ent5 = tk.Entry(self)
self.ent5.grid(row=18,column=2)
cls_btn = tk.Button(self,text='close', command=self.cls_win)
cls_btn.grid(row = 30, column = 2)
lbE = tk.Label(self,text='Youngs Modulus')
lbE.grid(row=10,column=1)
lbD = tk.Label(self,text='Outer diameter')
lbD.grid(row=12,column=1)
lbd = tk.Label(self,text='Inner diameter')
lbd.grid(row=14,column=1)
lbL = tk.Label(self,text='Length')
lbL.grid(row=16,column=1)
lbw = tk.Label(self,text='UDL')
lbw.grid(row=18,column=1)
def btns(self):
self.ent1 = float(self.ent1.get())
self.ent2 = float(self.ent2.get())
self.ent3 = float(self.ent3.get())
self.ent4 = float(self.ent4.get())
self.ent5 = float(self.ent5.get())
def cls_win(self):
self.destroy()
def main():
app = beam(None)
w, h = app.winfo_screenwidth(), app.winfo_screenheight()
app.geometry("%dx%d+0+0" % (w, h))
app.title('Beam app')
app.mainloop()
if __name__ == '__main__':
main()
我可以通过以下方式让每个输入框提交:
self.btn1 = tk.Button(parent, text='Submit', command=self.btns)
2 个回答
-1
你需要先创建一个输入框,然后最后把它放到网格中,这样才能正常工作。
比如说:
import tkinter,tkinter.messagebox,tkinter.simpledialog as tk
from tkinter import ttk
import sqlite3
large_font=("Verdana",12)
class way(tk.Tk):
def __init__(self,*args,**kwargs):
a=tk.Tk.__init__(self,*args,**kwargs)
container=tk.Frame(a)
container.grid(row=0,column=0)
self.menu = tk.Menu(a, tearoff=0)
self.menu.add_command(label="Beep", command=self.bell)
self.menu.add_command(label="Exit")
self.bind("<Button-3>", self.showMenu)
self.frames={}
for f in (pa1,pa2,pa3,pa4,pa5,pa6,pa7,pa8,pa9,pa10):
frame=f(container,self)
self.frames[f]=frame
frame.grid(row=0,column=0,sticky='nsew')
self.show_frame(pa1)
menu=tk.Menu(self)
self.config(menu=menu)
file=tk.Menu(menu)
file.add_command(label='Exit',command=self.over)
menu.add_cascade(label='File',menu=file)
def show_frame(self,cont):
frame=self.frames[cont]
frame.tkraise()
def over(self):
self.destroy()
def sample(self,ev):
print(ev)
def showMenu(self, e):
self.menu.post(e.x_root, e.y_root)
class pa1(tk.Frame):
def __init__(self,parent,con):
tk.Frame.__init__(self,parent);
a=ttk.Button(self,text='Dealer',command=lambda:con.show_frame(pa2)).grid(row=0,column=0)
b=ttk.Button(self,text='Branch',command=lambda:con.show_frame(pa3)).grid(row=1,column=0)
c=ttk.Button(self,text='Customer',command=lambda:con.show_frame(pa4)).grid(row=2,column=0)
#I like to use bind but it does not support please tell me the reason why it does not support it.
class pa2(tk.Frame):
def __init__(self,parent,con):
tk.Frame.__init__(self,parent);
l1=tkinter.ttk.Label(self,text='Enter dealer id :',width=20)
l2=tkinter.ttk.Label(self,text='Enter dealer password :',width=20)
e1=tkinter.ttk.Entry(self)
e2=tkinter.ttk.Entry(self)
print(type(e1),type(e2),type(tkinter.ttk.Entry()))
b1=ttk.Button(self,text="Back",command=lambda:con.show_frame(pa1)).grid(row=2,column=0)
b2=ttk.Button(self,text='Submit',command=lambda:self.check(e1,e2)).grid(row=2,column=1)
l1.grid(row=0,column=0)
l2.grid(row=1,column=0)
e1.grid(row=0,column=1)
e2.grid(row=1,column=1)
def check(self,e1,e2):
try:
a=e1.get()
b=e2.get()
except Exception as e:
print(e)
else:
print('No error')
class pa3(tk.Frame):
def __init__(self,parent,con):
tk.Frame.__init__(self,parent);
b=ttk.Button(self,text="back",command=lambda:con.show_frame(pa1)).grid(row=0,column=0)
class pa4(tk.Frame):
def __init__(self,parent,con):
tk.Frame.__init__(self,parent);
b=ttk.Button(self,text="back",command=lambda:con.show_frame(pa1)).grid(row=0,column=0)
class pa5(tk.Frame):
def __init__(self,parent,con):
tk.Frame.__init__(self,parent);
class pa6(tk.Frame):
def __init__(self,parent,con):
tk.Frame.__init__(self,parent);
class pa7(tk.Frame):
def __init__(self,parent,con):
tk.Frame.__init__(self,parent);
class pa8(tk.Frame):
def __init__(self,parent,con):
tk.Frame.__init__(self,parent);
class pa9(tk.Frame):
def __init__(self,parent,con):
tk.Frame.__init__(self,parent);
class pa10(tk.Frame):
def __init__(self,parent,con):
tk.Frame.__init__(self,parent);
a=way()
a.mainloop()
3
在你下面的这个函数代码中,有一个明显的问题:
def btns(self):
self.ent1 = float(self.ent1.get())
self.ent2 = float(self.ent2.get())
self.ent3 = float(self.ent3.get())
self.ent4 = float(self.ent4.get())
self.ent5 = float(self.ent5.get())
这里你把 self.ent?
的属性给覆盖了,这样的话你只能执行一次 btns
。如果你再执行一次,就会出现 AttributeError
的错误,具体来说是:
AttributeError: 'float' object has no attribute 'get'
关于你的问题,你已经通过一个按钮获取了所有输入框的值,所以很难判断具体是什么问题。不过,你描述的一个简单示例可能是:
import Tkinter as tk
root = tk.Tk()
e1 = tk.Entry(root)
e1.pack()
e2 = tk.Entry(root)
e2.pack()
# This function is executed by the submit button
# it retrieves the outputs of both entry boxes
def submit():
print e1.get()
print e2.get()
tk.Button(root,text="submit",command=submit).pack()
root.mainloop()