Dynamically changing button text in python

M

marijuanated

Hi all,

I am wondering if i could change a button text dynamically in its
handler.

for example,can we do something like this:

curButton.bind("<Button-1>",self.StopServer)

def StopServer(self,event):
curButton["text"] = "Start Server"

Thanks,
Sundar
 
P

Paul Rubin

for example,can we do something like this:

curButton.bind("<Button-1>",self.StopServer)

def StopServer(self,event):
curButton["text"] = "Start Server"

Yes.
 
M

marijuanated

The problem is once i set the textvariable property of the button,i
cannot change the text.For example,

curButton = Button(root,text="Stop Server",textvariable="this")

curButton.bind("<Button-1>",self.StopServer)

def StopServer(self,event):
curButton["text"] = "Start Server" #this does not work
 
P

Paul Rubin

The problem is once i set the textvariable property of the button,i
cannot change the text.For example,

curButton = Button(root,text="Stop Server",textvariable="this")

1. I don't think you can use text and textvariable at the same time.
2. I don't think you're using textvariable the right way.

Try something like:

tv = StringVar()
curButton = Button(root, textvariable=tv)
curButton.bind("<Button-1>",self.StopServer)
def StopServer(self,event):
tv.set("Start Server")

--Paul
 
F

Fredrik Lundh

The problem is once i set the textvariable property of the button,i
cannot change the text.For example,

curButton = Button(root,text="Stop Server",textvariable="this")

what made you think that setting textvariable to "this" would be a good
idea?

the textvariable option is used to specify a Tkinter variable that's used
to control the button text; e.g.

curVar = Tkinter.StringVar()
curButton = Button(root, textvariable=curVar)

curVar.set("new text")

also see

http://effbot.org/tkinterbook/button.htm
http://effbot.org/tkinterbook/variable.htm

</F>
 
M

marijuanated

Thank you very much.I had actually misinterpreted the "textvariable"
property for some kind of "storage for each Button". I am actually
developing an app where multiple buttons have a single event handler.So
i thought the "textvariable" property might be used to store some info
about each of the Button so that the handler can differentiate between
the callers by examining the property.Now i achieved similar
functionality with callbacks because button command handlers dont
receive any additional arguments.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,276
Messages
2,571,384
Members
48,073
Latest member
ImogenePal

Latest Threads

Top