#๐Ÿ”’ Accesing variable from another def

12 messages ยท Page 1 of 1 (latest)

halcyon solstice
#

Code: ```py
class App(customtkinter.CTk):
def init(mainmenu, *args, **kwargs):
super().init(*args, **kwargs)
mainmenu.geometry("750x450")
customtkinter.set_appearance_mode("dark")
customtkinter.set_default_color_theme("blue")

    mainmenu.title("Prijavi se")
    mainmenu.button_1 = customtkinter.CTkButton(mainmenu, text="Prijavi se", command= lambda: mainmenu.open_loginwindow())
    mainmenu.button_1.pack(side="top", padx=20, pady=20)

    mainmenu.toplevel_window = None



def open_loginwindow(app):
        app.toplevel_window = LoginWindow(app)
        app.mainmenu.destroy()
        

def login_success(parent):
        parent.toplevel_window = MainWindow(parent)```

When i run app,it opens first window which i called mainmenu and i want when button is clicked and def open_loginwindow is runned to at same time destroy mainmenu and show LoginWindow

solar turretBOT
#

@halcyon solstice

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.

halcyon solstice
#

Accesing variable from another function

sand canopy
#

Can you explain more clearly what you're trying to achieve?

delicate sail
#

You can share variable between functions in a class with self

#

!self

solar turretBOT
#
Class instance

When calling a method from a class instance (ie. instance.method()), the instance itself will automatically be passed as the first argument implicitly. By convention, we call this self, but it could technically be called any valid variable name.

class Foo:
    def bar(self):
        print('bar')

    def spam(self, eggs):
        print(eggs)

foo = Foo()

If we call foo.bar(), it is equivalent to doing Foo.bar(foo). Our instance foo is passed for us to the bar function, so while we initially gave zero arguments, it is actually called with one.

Similarly if we call foo.spam('ham'), it is equivalent to
doing Foo.spam(foo, 'ham').

Why is this useful?

Methods do not inherently have access to attributes defined in the class. In order for any one method to be able to access other methods or variables defined in the class, it must have access to the instance.

Consider if outside the class, we tried to do this: spam(foo, 'ham'). This would give an error, because we don't have access to the spam method directly, we have to call it by doing foo.spam('ham'). This is also the case inside of the class. If we wanted to call the bar method inside the spam method, we'd have to do self.bar(), just doing bar() would give an error.

delicate sail
#
class A:
   def __init__(self):
      self.a="a" 
   def print(self):
      print(self.a)
#

Like this

#

Oh, you got help from #python-discussion

solar turretBOT
#
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.