【问题标题】:Why do I have to click the button more than once? tkinter gui为什么我必须多次单击按钮? tkinter gui
【发布时间】:2014-05-06 07:45:19
【问题描述】:

您好,我使用 tkinter 创建了一个 GUI,它接受用户的输入。 我创建了自己的存在检查和“拼写”检查,以确保用户只输入可接受的值。 但是每次我输入这些值时,它都会让用户在输出屏幕出现之前多次单击提交按钮。 这是因为我使用了多个功能吗? 有没有其他方法可以做到这一点?

def Search():
    global E1, E2, E3, E4, file

    def PresenceValidation():

        if E1.get() ==(''):
            root2=Tk()
            errortext = Label(root2, text = "Please enter eye colour")  
            errortext.pack()
        elif E2.get() ==(''):
            root2=Tk()
            root2.configure(bg="white")
            errortext = Label(root2, text = "Please enter age")
            errortext.pack()
        elif E3.get() ==(''):
            root2=Tk()
            root2.configure(bg="white")
            errortext = Label(root2, text = "Please enter hair colour")
            errortext.pack()
        elif E4.get() ==(''):
            root2=Tk()
            root2.configure(bg="white")
            errortext = Label(root2, text = "Please enter shoesize")
            errortext.pack()            
        else:
            button = Button(root, text = "Submit information", command=SpellCheck)
            button.grid(row=3, column=2, padx = 5)

    def SpellCheck():
        if E1.get().lower() not in ('blue', 'brown', 'green'):
            root2=Tk()
            root2.configure(bg="white")
            errortext = Label(root2, text = "Please enter eye colour")
            message= Label(root2, text = "Blue, Brown or Green")
            errortext.pack()
            message.pack()
        elif E2.get().lower()  not in ('10-20', '20-30', '30-40','40+'):
            root2=Tk()
            root2.configure(bg="white")
            errortext = Label(root2, text = "Please enter a valid age group")
            message=Label(root2, text = "10-20, 20-30, 30-40 or 40+")
            errortext.pack()
            message.pack()
        elif E3.get().lower()  not in ('Brown', 'Black', 'Blonde', 'Auburn'):
            root2=Tk()
            root2.configure(bg="white")
            errortext = Label(root2, text = "Please enter a valid hair colour")
            message = Label(root2, text = "Brown, Black, Blonde or Auburn")
            errortext.pack()
            message.pack()
        elif E4.get().lower() not in ('1', '2', '3','4','5','6','7','8+'):
            root2=Tk()
            root2.configure(bg="white")
            errortext = Label(root2, text = "Please enter a valid shoesize")
            message = Label(root2, text = "1,2,3,4,5,6,7,8+")
            errortext.pack()
            message.pack()         
        else:
            submitbutton = Button(window, text = "Submit information", command=Submit)
            submitbutton.grid(row=3, column=2, padx = 5)   


    root = Tk()
    root.title ("Search for matches")



    #text that will be shown to the user
    root.configure(bg="white")
    Label(root, text="Please enter eye colour").grid(row=0)
    Label(root, text="Please enter age").grid(row=1)
    Label(root, text="Please enter hair colour").grid(row=2)
    Label(root, text="Please enter shoesize").grid(row=3)


    #Create user entry points
    E1= Entry(root)
    E2= Entry(root)
    E3= Entry(root)
    E4= Entry(root)
    # locating input boxes to area on the screen
    E1.grid(row=0, column=1)
    E2.grid(row=1, column=1)
    E3.grid(row=2, column=1)
    E4.grid(row=3, column=1)




    submitbutton = Button(window, text = "Submit information", command=PresenceValidation)
    submitbutton.grid(row=3, column=2, padx = 5)

    def Quit():
    window.destroy()

    quitbton = Button (window, text ="QUIT", command=Quit)
    quitbton.grid(row=3, column=3, padx=5)

【问题讨论】:

  • 您有 2 个按钮,其文本完全相同 Submit information 和位置,甚至还有 submitbutton 变量
  • 那是因为它们都导致相同的输出函数
  • 问题是它们位于完全相同的位置。所以它们相互重叠。由于这个原因,现在你必须不得不按两次按钮
  • 至少改变行或列
  • 对不起三次(我数错了:P)

标签: python user-interface tkinter


【解决方案1】:

您的部分问题是您正在创建多个根窗口。 Tkinter 的设计并非如此。如果您需要更多窗口,请创建 Toplevel 的实例。

【讨论】:

    【解决方案2】:

    您可以重写此代码以省略大量代码,并使其按照您想要的方式运行。您获得多个按钮的原因是因为您正在制作多个按钮。三个,如果我把它们都数一遍的话。初始的Submit 按钮应该调用一个函数来验证字段并在遇到不正确的值时跳出循环。您也可以使用Messagebox 来提示用户错误所在。

    如果您不添加 Messagebox 并希望保持原样,则应将 root2 实例更改为 Toplevel 小部件以避免创建两个 Tkinter 实例。这也可能导致意外结果。

    下面是一个示例,说明您可以如何简单地进行状态检查:

    from tkMessageBox import showwarning
    
    def PresenceValidation():
        #Make dictionary to hold values
        values = {'Eye color': E1.get(),
                  'Age': E2.get(),
                  'Hair': E3.get(),
                  'Shoesize': E4.get()
                  }
    
        #Iterate over dictionary to check for empty items
        for k,v in values.iteritems():
            if v == '':
                showwarning('Empty Value Alert',    #Create alert message
                            k + ' needs a value')   #for the first empty value,
                break                               #and break out of the loop
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-29
      • 2021-12-30
      • 1970-01-01
      • 1970-01-01
      • 2013-01-10
      • 1970-01-01
      • 1970-01-01
      • 2015-07-02
      相关资源
      最近更新 更多