Questions & Answers

Exit Function Into Main Code

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?

2023-01-25 00:30:16
You need to post the code that you are having an issue with if you want a reasonable answer
2023-01-25 00:30:16
What you are talking about should be automatic. Unless you have some kind of loop in your function that is forever looping.
Answers(2) :

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.

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()


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.

2023-01-25 00:30:16
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?
2023-01-25 00:30:16
@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.
2023-01-25 00:30:16
I destroyed the labels so something else could take the screen. Maybe there is a way to hide them and then unhide them?
2023-01-25 00:30:16
@CadenGrey you can use grid_forget() and then replace the widget instead of destroying it.
2023-01-25 00:30:16
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/", line 2039, in place_configure + self._options(cnf, kw)) _tkinter.TclError: bad window path name ".140504410151904" '
2023-01-25 00:30:16
@CadenGrey that's not something we can help with unless we can see your code.
2023-01-25 00:30:16
The code is too long. This is the full code. The place functions make a dice picture come across the window. it works the first time, but after the widgets are replaced the images do not display, but the rest of the code works.
2023-01-25 00:30:16
@CadenGrey I am on my phone right now. Look up Minimal Complete and Verifiable Example so you can learn how to ask a question on stack overflow.
2023-01-25 00:30:17
@CadenGrey Sorry its been a busy week for me. Buying a house right now. I am not able to review your code from work due to our firewall not allowing that site. If you can please provide a Minimal, Complete, and Verifiable example I can answer to the specifics of your problem.