【问题标题】:How can i do two different things with 2 buttons on python(GUI)?我如何在 python(GUI) 上使用 2 个按钮做两件不同的事情?
【发布时间】:2015-12-02 21:15:03
【问题描述】:

我想知道如何用 2 个按钮完成 2 件独立的事情。目前,即使我单击“提示”按钮,它也会执行其他按钮的操作。这是我的代码(尚未完成)。 导入 Tkinter

    class simpleapp_tk(Tkinter.Tk):
        def __init__(self,parent):
            Tkinter.Tk.__init__(self,parent)
            self.parent = parent
            self.initialize()

        def initialize(self):
            self.grid()

            self.entryVariable = Tkinter.StringVar()

            self.entry = Tkinter.Entry(self)
            self.entry.grid(column=1,row=1,sticky='EW')
            self.entry.bind("<Return>", self.OnPressEnter)







            button = Tkinter.Button(self,text=" Click here plz..!",
                                    command=self.OnButtonClick)
            button.grid(column=1,row=4)

            self.entry2=Tkinter.Entry(self)

            button2 = Tkinter.Button(self,text="Hint?",
                                     command=self.OnButtonClick)
            button2.grid(column=2,row=4)

            self.resizable(True,False)
        def OnButtonClick(self):
            print"Your answer is :"
            print self.entry.get()
            if self.entry.get()== "4":
                print "GJ"
            elif self.entry.get()==" ":
                print "please enter a valid answer"
            else :
                print "Nop"

        def secndButtonClick(self):
            if self.entry2.get()=="":
                print "here is the hint"
            else:
                print"Don't write anything here"

        def OnPressEnter(self,event):
            print"Your answer is :"
            print self.entry.get()
            if self.entry.get()=="4":
                print" GJ, let me guess...You have more than 2 years old right?"
            elif self.entry.get()==" ":
                print "please enter a valid answer"
            else :
                print "Nop...you are the stupidiest person I know..."
            if self.entry2.get()==" ":
                print" Ok"
    print" 2+2=?"
    if __name__ == "__main__":
        app = simpleapp_tk(None)
        app.title('IQ test!')
        app.mainloop()

【问题讨论】:

  • 您的两个按钮命令都指向 self.OnButtonClick... 您期望会发生什么...?显然他们都会执行那个方法。

标签: python user-interface button onclick assign


【解决方案1】:

第二个按钮的命令与第一个相同。我对 Tkinter 没有太多经验,但这应该可以工作

button2 = Tkinter.Button(self,text="Hint?",
                         command=self.secndButtonClick)

【讨论】:

    猜你喜欢
    • 2016-01-22
    • 1970-01-01
    • 2019-03-07
    • 1970-01-01
    • 2013-07-19
    • 1970-01-01
    • 2017-12-31
    • 2023-03-27
    • 1970-01-01
    相关资源
    最近更新 更多