user46646
user46646

Reputation: 159261

Tkinter button bind

This is my code:

import Tkinter
from Tkconstants import *

tk = Tkinter.Tk()


class MyApp:

    def __init__(self,parent):

        self.frame = Tkinter.Frame(tk,relief=RIDGE,borderwidth=2)
        self.frame.pack()

        self.message = Tkinter.Message(tk,text="Symbol Disolay")

        label=Tkinter.Label(self.frame,text="Is Symbol Displayed")
        label.pack()

        self.button1=Tkinter.Button(self.frame,text="YES")
        self.button1.pack(side=BOTTOM)
        self.button1.bind("<Button-1>", self.button1Click)

        self.button2=Tkinter.Button(self.frame,text="NO")
        self.button2.pack()
        self.button2.bind("<Button-1>", self.button2Click)


    def button1Click(self, event):
            "pressed yes"

    def button2Click(self, event):
            "pressed no"

myapp = MyApp(tk)
tk.mainloop()

What shall I do in button1Click() and button2Click() so that they return "YES" or "NO" to my program in string format?

Upvotes: 2

Views: 29516

Answers (3)

Ramchandra Apte
Ramchandra Apte

Reputation: 4079

import Tkinter
from Tkconstants import *

tk = Tkinter.Tk()


class MyApp:

    def __init__(self,parent):

        self.frame = Tkinter.Frame(tk,relief=RIDGE,borderwidth=2)
        self.frame.pack()

        self.message = Tkinter.Message(tk,text="Symbol Disolay")

        label=Tkinter.Label(self.frame,text="Is Symbol Displayed")
        label.pack()

        self.button1=Tkinter.Button(self.frame,text="YES")
        self.button1.pack(side=BOTTOM)
        self.button1.bind("<Button-1>", self.button1Click)

        self.button2=Tkinter.Button(self.frame,text="NO")
        self.button2.pack()
        self.button2.bind("<Button-1>", self.button2Click)


    def button1Click(self, event):
            return "YES"

    def button2Click(self, event):
            return "NO"

myapp = MyApp(tk)
tk.mainloop()

Upvotes: 0

Bryan Oakley
Bryan Oakley

Reputation: 385830

You can't "return" a string, because the callbacks are called from the event loop and the event loop ignores all values returned from callbacks since it doesn't have any knowledge of what to do with the returned values.

If you want "YES" or "NO" to be applied to the self.message widget, you can change your callbacks to look like this:

def button1Click(self, event):
        self.message.configure(text="YES")

def button2Click(self, event):
        self.message.configure(text="NO")

You'll then need to make the message widget visible. For example, add "self.message.pack()" in your code somewhere.

If you need a more specific answer to your question you'll need to do a better job of describing what you mean by "return" -- do you want to see the text in a label, appear in a popup dialog, etc.

Upvotes: 2

Will
Will

Reputation: 75615

simply return "pressed yes" will return the string to the TKinter handler, which'll ignore it.

You have to do something in your click defs, e.g. update the label text

Upvotes: 1

Related Questions