【问题标题】:Tkinter-how to 'get' data values from more than one entry box using one button?Tkinter-如何使用一个按钮从多个输入框中“获取”数据值?
【发布时间】:2014-05-29 14:14:49
【问题描述】:

只是在我正在练习的一个小 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)

【问题讨论】:

  • ebarr - 感谢您的编辑!我确信这次我做对了......
  • 您已经有了一个通过一个按钮 (btns) 获取所有值的函数。您覆盖了小部件引用,但实际上您的代码只需单击一次按钮即可获取所有值。
  • @Bryan Oakley 的确如此!我设法混淆了自己,并没有意识到如何使用它......干杯

标签: python user-interface tkinter


【解决方案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())

这里你将覆盖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()

【讨论】:

  • 感谢您的意见。上面的代码确实可以正常工作,我还可以使用每个输入框的单独按钮来检索文本。我正在寻找的是一种仅使用一个按钮而不是五个(当前)按钮来检索输入的方法。如果不清楚,我们深表歉意,并感谢您的建议。
【解决方案2】:

您必须先创建条目框,最后将其网格化。 例如

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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多