0

Experimenting with Tkinter and Gui creating and cant seem to run two commands off of one button press.

I've already tried moving the second definition out of the first definition to the same error as before. The problem is that it is a separate window to my main window and so is under a different definition meaning. When I've tried the previous threads answers it isn't working for me.

def cheese():
    fll = Tk()

    #The two commands linked together
    def fildes():
        filling2()
        fll.destroy

    fll.title=("Test")

    ll = Label(fll, text ="Would you like to choose another topping?").pack()
    #The button under this runs fildes()
    bb = Button(fll, text ="Yep", command = fildes).pack()
    bbb = Button(fll, text ="No", command = fll.destroy).pack()

It should create a window and when the yes button is pressed open a new window while closing the current window.

It opens the new window but fails too delete the current window due to fll not being defined

    def fildes():
        filling2()
        fll.destroy()

this does not work either

Doppy
  • 11
  • 7
  • 2
    Possible duplicate of [Have multiple commands when button is pressed](https://stackoverflow.com/questions/13865009/have-multiple-commands-when-button-is-pressed) – CommonSense Dec 20 '18 at 09:46
  • I had a look at that that's why I wrote this none of those answers seem to work for me? I think its because its inside a definition which It needs to be as it comes from another button to this one. – Doppy Dec 20 '18 at 09:57
  • in above code, you are not _calling_ `destroy` inside `fildes`. – Lafexlos Dec 20 '18 at 10:25
  • Okay so is there anyway for me to call destroy inside fildes? – Doppy Dec 20 '18 at 10:27
  • Should be `fll.destroy()`, not `fll.destroy` inside `fildes`. – acw1668 Dec 20 '18 at 10:31
  • Thanks ill try that now. It doesn't work for some reason the tab stays open. – Doppy Dec 20 '18 at 10:31

1 Answers1

0

Solution:

Always try to pass the arguments to your functions. If I'm seeing this right, Tkinter only calls the standalone filedes() function, without any knowledge about local variables in cheese.

Pass the window you want destroyed to filedes like this:

def fildes(old:Tk):
    old.destroy()
    filling2()

Then add fll as parameter to the command of your button. (Can be done like this.)

bb = Button(fll, text ="Yep", command = lambda: fildes(fll))


PS:

When I tried to run your code I had to call .pack() like this or else it wouldn't work:

sample = Button()
sample.pack()
kilian579
  • 68
  • 2
  • 11