|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
AT&T devCentral & BlackBerry(r) Webcast Series: BlackBerry and GPS -Build Location Awareness into your BlackBerry Applications, July 10th-1:00PM EST. Register Today!
|
|
#1
|
||||
|
||||
|
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()
__________________
IE QUOTE | PHP Manual | Google | C/C++ Compiler | Linux Tutorials | General Stuff Game Dev |
|
#2
|
||||
|
||||
|
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 ![]()
__________________
*** Experimental Python Markup CGI V2 *** |
|
#3
|
||||
|
||||
|
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
![]() |
|
#4
|
||||
|
||||
|
hehe nice.. good thing i was close
for now on i will just something like.. what you didCode:
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 ![]() |
|
#5
|
|||
|
|||
|
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)) |
|
#6
|
||||
|
||||
|
__________________
It is not important if the glass is half full or half empty.What is important,is who has been drinking from MY glass?!?!? |
|
#7
|
||||
|
||||
|
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 |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Tkinter button events |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|