#๐Ÿ”’ Simple Name Error, Can't Figure It Out

41 messages ยท Page 1 of 1 (latest)

maiden sirenBOT
#

@tepid owl

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

stoic rapids
#

700 lines? really?

name the line where this error pops up, first

shell sphinx
#
import tkinter as tk
from tkinter import ttk
from tkinter import *

...

tepid owl
#

line 181, in autowork
self.update_debt(3)
line 242, in update_debt
self.update_all_tabs()
line 639, in update_all_tabs
finance_tab.update_money_display()
^^^^^^^^^^^
NameError: name 'finance_tab' is not defined. Did you mean: 'FinanceTab'?

stoic rapids
tepid owl
#

yeah idk moving the update_all_tabs() function below the tab creation just creates another error, and for some reason the first tab "street" isn't returning an error at all

#

even though it's also created after the function is used

shell sphinx
#

you need a variable defined at the time that a function is ran

stoic rapids
tepid owl
#

but yeah repositioning the function is useless

shell sphinx
#
class StreetTab(tk.Frame):
    def __init__(self, notebook, update_all_tabs): ...

my IDE is telling me that notebook is unused in the init

fallen plinth
#

The error is because of how you have things set up. StreetTab's __init__ calls autowork, autowork calls update_time, and update_time calls update_all_tabs. Granted, autowork is called in a delayed callback, but if it runs before finance_tab = has run, that will cause that error.

tepid owl
#

Exactly

#

So what do I do? Moving the function doesn't work

fallen plinth
#

First, I don't see why you're passing that function around in the first place. update_all_tabs is a global function (not the problem, just odd).

tepid owl
#

Uh well I was having trouble getting variables synchronized across tabs

#

So I created a function that calls to update those variables across the tabs since each tab is a class

fallen plinth
#

Unless you do a large refactor, you might just need to do something like give the Tab classes a start method that kicks off the after(self.autowork()) calls so you can control when update_all_tabs is called.

tepid owl
#

Like kinda delay it for a moment so it can get to the tab creation?

fallen plinth
#

That's not great since that's effectively a second initializer that you need to remember to call, but I don't see how else you'd fix this with how the code is set up currently/

tepid owl
#

Yeah this is definitely a learning experience trying to throw it together for a simple idle game, it's all scope errors and this is my third attempt

fallen plinth
#

Actually, it seems like all the start method would need to do it change the state of autowork_enabled.

#

And then you'd just start that BooleanVar as False.

tepid owl
#

omg that worked

fallen plinth
#

Actually, that might be an abuse of that state.

tepid owl
#

Thank you so much

#

Well, it at least got rid of the error, and I see why

fallen plinth
tepid owl
#

Yeah big thanks, that was driving me crazy. Should I close this question out?

fallen plinth
#

Might as well. You can always open another thread later if you need to.

tepid owl
#

Aight thanks again

fallen plinth
#

Also, FYI, that site you linked to is kind of a pain to use since it loads lazily, which breaks search and scrolling. This would be better:

#

!paste

maiden sirenBOT
#
Pasting large amounts of code

If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/

After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.

fallen plinth
#

For future reference.

tepid owl
#

Bookmarked, appreciate it

maiden sirenBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.