繁体   English   中英

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

[英]How can i do two different things with 2 buttons on python(GUI)?

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

您第二个按钮的命令与第一个相同。 我对Tkinter的经验不足,但这应该可以

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM