The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Tkinter button events
Discuss Tkinter button events in the Python Programming forum on Dev Shed. Tkinter button events Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

March 11th, 2004, 02:21 PM
|
 |
Only the strong survives!!.
|
|
Join Date: Feb 2003
Location: A World of wonders.
|
|
|
Tkinter button events
Im tring to to create this little practice program that ... when you click on OK button another button shows up but it does not seem to be working or i dont know how to do it right.. any ideas?
Code:
from Tkinter import *
class GUI:
def __init__ (self, master):
frame = Frame(master)
frame.pack()
self.label = Label(frame,{"text":"This is just a test program"})
self.label.pack()
self.button = Button(frame)
self.button.configure(text="OK",background="blue",foreground="white",command=self.buttonClick(frame));
self.button.pack(side=LEFT)
def buttonClick(self,master):
self.button1 = Button(master,text="Click")
self.button1.pack(side=LEFT)
if __name__ == "__main__":
root = Tk()
root_gui = GUI(root)
root.mainloop()
|

March 11th, 2004, 03:33 PM
|
 |
Mini me.
|
|
Join Date: Nov 2003
Location: Cambridge, UK
|
|
You were close....
You have to give your Tkinter button a reference to the function that is to be called - so just it's name not the complete thing
In which case you need to track the main root window so you can hang buttons on it. The easiest way is to use an instance variable like self.master. This restriction is a major reason why most Tkinter programs are base around classes.
Code:
from Tkinter import*
class GUI:
def __init__(self, master):
self.master = master
frame = Frame(self.master)
frame.pack()
self.label = Label(frame, {"text": "This is just a test program"})
self.label.pack()
self.button = Button(frame)
self.button.configure(text = "OK", background = "blue",
foreground = "white", command = self.buttonClick);
self.button.pack(side = LEFT)
def buttonClick(self):
self.button1 = Button(self.master, text = "Click")
self.button1.pack(side = LEFT)
if __name__ == "__main__":
root = Tk()
root_gui = GUI(root)
root.mainloop()
Grim 
|

March 11th, 2004, 03:42 PM
|
 |
Mini me.
|
|
Join Date: Nov 2003
Location: Cambridge, UK
|
|
BTW out of interest, what text editor are you using? Your tabs are 3 chars and not 4. Not a problem if you only write your own stuff but it might get messy if you try pick-a-mix coding 
|

March 11th, 2004, 03:45 PM
|
 |
Only the strong survives!!.
|
|
Join Date: Feb 2003
Location: A World of wonders.
|
|
hehe nice.. good thing i was close  for now on i will just something like.. what you did
Code:
self.master = master
im using context on windows.. do you have anything better? as im looking fro a better text editor.. btw how did you add them colors to your code tags 
|

March 11th, 2004, 05:18 PM
|
|
Contributing User
|
|
Join Date: Jan 2004
Posts: 84
Time spent in forums: 8 h 7 m
Reputation Power: 10
|
|
you could also use a lambda function, if you want to avoid creating too many instance variables (though the root window is pretty much always one you will want to give your entire class access to).
Code:
Button(self, text="Test", command=lambda: self.somefunction(somevariable))
|

March 11th, 2004, 05:57 PM
|
 |
Contributing User
|
|
Join Date: Jan 2004
Posts: 103
Time spent in forums: 3 Days 1 h 17 m 20 sec
Reputation Power: 10
|
|
Code:
print 'I think he used his py2html'
print 'You can find it :'
Here
__________________
It is not important if the glass is half full or half empty.What is important,is who has been drinking from MY glass?!?!?
|

March 11th, 2004, 06:25 PM
|
 |
Mini me.
|
|
Join Date: Nov 2003
Location: Cambridge, UK
|
|
You don't have to like my colours but if you use IDLE you might recognise them  . PHP's built in colouriser is  . I wanted something like it but that could generate more or less any markup. The GUI layout is a bit Mickey Mouse but I'm working on it, in fact I'm planning a complete re-work of most of the code
I prefer TextPad as an editor but I do use IDLE a lot.
Grim
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|