1

guys. I'm writing some code for a school project, and I'm getting a basic application planned out. I'm trying to keep a picture in the def but the image remains after you switch. How can I get it to go away when you change functions? My code:

def acceleration():
    var.set("Acceleration \n \n \n \n \n \n Acceleration is defined as: \n 'the increase in the rate or speed of something. \n How could this be used in code?' \n \n \n")
    photo = PhotoImage(file="accel.gif")
    img = Label(root, image=photo)
    img.photo = photo
    img.pack();

Thanks Guys! ~Brayden

P.S. I'll gladly give you the entire script if necessary, though I think it won't.

  • I would create the label in your main and pass its variable through to the function, then each function, when called, will change the value of the label. This would be a 'pass by reference' in proper computational terms. – Dylan Lawrence Nov 19 '13 at 23:59
  • Did you mean to put the var.set("Blah Blah") as a variable, then remove the var.set in the function, and replace it with the variable I gave it? IE: v = var.set("Blah Blah") In the function: def acceleration(): photo = PhotoImage(file="accel.gif") img = Label(root, image=photo) img.photo = photo img.pack(); –  Nov 20 '13 at 00:08
  • No, I mean, in your main, do img = Label(root, image=None) and for each function you'll have it as a paramater. i.e. def acceleration(img): this allows all your functions access to the same label. In your code, the label exists in tkinter, but the variable referencing it is destroyed when the function ends. – Dylan Lawrence Nov 20 '13 at 00:09
  • I've done that, but I get this error: Exception in Tkinter callback Traceback (most recent call last): File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1437, in __call__ return self.func(*args) TypeError: acceleration() takes exactly 1 argument (0 given) –  Nov 20 '13 at 00:24
  • when you call acceleration are you passing the variable through, or is this function being called by a button or some other tk object. If that's the case, I believe you have to use a special setup to pass parameters to the function. – Dylan Lawrence Nov 20 '13 at 00:27
  • Yes, I am using a button.. I didn't guess that'd be important. I can put the script on github for you to look at if you'd like? –  Nov 20 '13 at 00:29
  • this should answer the issue with parameter passing, http://stackoverflow.com/questions/6922621/how-can-i-pass-arguments-to-tkinter-buttons-callback-command – Dylan Lawrence Nov 20 '13 at 00:31
  • Thanks mate! You were a big help! –  Nov 20 '13 at 00:58

0 Answers0