如何在python(GUI)上使用2个按钮执行两个不同的操作?

时间:2015-12-02 21:15:04

标签: python user-interface button onclick assign

我想知道如何用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()

1 个答案:

答案 0 :(得分:2)

您的第二个按钮的命令与第一个按钮的命令相同。我对Tkinter没有多少经验,但这应该有效

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