只是在我正在练习的一个小GUI上寻找一些建议。 我试图制作一个用于弯曲时刻的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)
答案 0 :(得分: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()
答案 1 :(得分:-1)
你必须首先创建Entry框,最后将它格式化。 例如
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()