Dynamically changing button text in python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • marijuanated@gmail.com

    Dynamically changing button text in python

    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.StopSe rver)

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

    Thanks,
    Sundar

  • Paul Rubin

    #2
    Re: Dynamically changing button text in python

    marijuanated@gm ail.com writes:[color=blue]
    > for example,can we do something like this:
    >
    > curButton.bind( "<Button-1>",self.StopSe rver)
    >
    > def StopServer(self ,event):
    > curButton["text"] = "Start Server"[/color]

    Yes.

    Comment

    • marijuanated@gmail.com

      #3
      Re: Dynamically changing button text in python

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

      curButton = Button(root,tex t="Stop Server",textvar iable="this")

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

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

      Comment

      • Paul Rubin

        #4
        Re: Dynamically changing button text in python

        marijuanated@gm ail.com writes:[color=blue]
        > The problem is once i set the textvariable property of the button,i
        > cannot change the text.For example,
        >
        > curButton = Button(root,tex t="Stop Server",textvar iable="this")[/color]

        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.StopSe rver)
        def StopServer(self ,event):
        tv.set("Start Server")

        --Paul

        Comment

        • Fredrik Lundh

          #5
          Re: Dynamically changing button text in python

          marijuanated@gm ail.com wrote:
          [color=blue]
          > The problem is once i set the textvariable property of the button,i
          > cannot change the text.For example,
          >
          > curButton = Button(root,tex t="Stop Server",textvar iable="this")[/color]

          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.StringV ar()
          curButton = Button(root, textvariable=cu rVar)

          curVar.set("new text")

          also see




          </F>



          Comment

          • marijuanated@gmail.com

            #6
            Re: Dynamically changing button text in python

            Thank you very much.I had actually misinterpreted the "textvariab le"
            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 "textvariab le" 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.

            Comment

            Working...