On 4/28/06, Ilknur Ozturk <[EMAIL PROTECTED]> wrote:
If I can use if statement to determine the which function will be called
when the button presses, it will be very helpful. Then I can call any
function by checking the button's attributes with if statement. If it is not
possible, how can I realize such an aim. If we can use if statement for
button's command option, can you give me an example?

Marhava, Ilknur-bey:

In tkinter program, it will be helpful to think about states rather
than conditions, since the GUI is event-driven.  So instead of an if
statement 'per se', think of telling the application whether you are
in an initial state or a subsequent state.

Here's a little app that accomplishes what you are after.  The app
also illustrates some elements of good Tkinter program design practice
(at least, as far as I understand them!) :-)

cheers,
Stewart
#!/usr/bin/python
#buttonpress.py

from Tkinter import *

class ButtonPress:
    def __init__(self):
        """init some vars"""
        self.firstTime = 1

    def draw_gui(self):
        """draw the user interface"""
        text1 = "press me!"
        b = Button(root, text=text1, command=self.buttonpress)
        b.pack()
        text2="First time is the best"
        l = Label(root, text=text2)
        l.pack()
        self.l = l

    def buttonpress(self):
        """deal with button presses"""
        if self.firstTime:
            self.firstTime = 0
            msg =  "Iyi gunlar!\nthe first time is the best"
            self.l.config(text=msg)
            #do some other first-time stuff here
        else:
            msg = "Tesekur ederim!\nyou can never go back to the first time"
            self.l.config(text=msg)
            #do some other non-first-time stuff here
        
if __name__ == "__main__":
        root = Tk()
        root.title("Button Press demo")
        root.geometry('300x100+400+400')
        btn = ButtonPress()
        btn.draw_gui()
        root.mainloop()
_______________________________________________
Tkinter-discuss mailing list
[email protected]
http://mail.python.org/mailman/listinfo/tkinter-discuss

Reply via email to