0

I am trying to build a gambling dice game for fun using Python's Tkinter in Python 3. The error I am having is after the money is taken away from your bank account (it does this in a different function) I want it to go back to the mainloop. So basically I want to exit a function to get back into the main code (which isn't in a function). Any ideas on how?

2
  • 1
    You need to post the code that you are having an issue with if you want a reasonable answer Commented Oct 23, 2017 at 21:46
  • 1
    What you are talking about should be automatic. Unless you have some kind of loop in your function that is forever looping. Commented Oct 23, 2017 at 21:49

2 Answers 2

1

I am not 100% sure what your problem is but it sounds like you might not fully understand how a function works.

In a tkinter application the mainloop() is always looping so once your function has finished the the next task in that loop will run and if no more task are scheduled in that loop the loop will reset.

Take this below code for example.

from tkinter import *

root = Tk()

def some_function():
    print("this function just ran")

Button(root, text="Run some func", command = some_function).pack()

root.mainloop()

What we see is a button that allows us to call on some_function and then once that function is over we are "back in" the mainloop() so to speak.

There is nothing you need to do special here unless inside of your function you are running some kind of loop and want to end the loop on some criteria. Then you can use a break line to end that loop.

Sign up to request clarification or add additional context in comments.

9 Comments

This may be just in my specific instance, but in the function I was talking about all of the labels I had are destroyed using the .destroy() function. So when the function is exited and it goes back to the mainloop nothing is there. Is it possible to get everything back?
@CadenGrey why did you destroy the labels if you planned to use them again? You can just set them to a blank value or something.
I destroyed the labels so something else could take the screen. Maybe there is a way to hide them and then unhide them?
@CadenGrey you can use grid_forget() and then replace the widget instead of destroying it.
Yep, that's what I did, but whenever the buttons are clicked after the replacement the code works, but I get an error like ' File "/usr/lib/python3.5/tkinter/__init__.py", line 2039, in place_configure + self._options(cnf, kw)) _tkinter.TclError: bad window path name ".140504410151904" '
|
0

The function will automatically stop when it is finished running. Using return, though, will immediately exit the current function, perhaps before it gets through all of its code, if that's what you mean.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.