1

Is it bad practise and/or is there a better way to be able to access function variables than this:

class Start_up:
    def __init__( self ):
        self.root       = Tk()
        self.user_entry = StringVar()
        self.change     = StringVar()

        self.label  = Label  ( self.root, textvariable = self.change     )
        self.entry  = Entry  ( self.root, textvaribale = self.user_entry )
        self.button = Button ( self.root, text="Buttton", command = self.doSomething )

    def doSomething( self ):
        self.change.set("Text Changed")
        got_it = self.user_entry.get()

I'm wondering if there is a way for these functions to work exactly the same but without the class being there?

Thank you in advance for any help, I've looked around but cant find precisely this example with TKinter.

tester
  • 415
  • 1
  • 5
  • 17
  • What are you trying to do, exactly? `self.entry` isn't used at all, nor is `got_it`. – chepner Nov 29 '14 at 15:23
  • Correct. This is just an example of what I want to do though. I want to be able to access the first functions tkinter variables from the second function, as previously said. – tester Nov 29 '14 at 15:28
  • Then you want a class, as the only alternative is to make the shared variables global, which is rarely recommended. Classes provide a way to share data between functions in a structured way. – chepner Nov 29 '14 at 15:29
  • Thanks for your super quick help! So the code I put would be recommended for passing variables between? Also would you recommend calling the window in __init__ or a new function in the class? – tester Nov 29 '14 at 15:32
  • Please read https://www.python.org/dev/peps/pep-0008 – jonrsharpe Nov 29 '14 at 15:37

2 Answers2

1

No, there is not a better way. What you've shown in your example is the right way to do it, as far as storing and accessing variables go.

You can do it without classes, of course. Your instance variables must then be global variables. In general, global variables should be avoided if possible. By using classes, your data is nicely encapsulated in the code that uses the data.

As for the rest of your example, I would recomment not creating the root window in the app class, though to some degree that is personal preference. My personal recommendation for program structure is here: https://stackoverflow.com/a/17470842/7432

Community
  • 1
  • 1
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
0

The basic idea is okay, but you should move the root outside the class.

class App:
    def __init__(self, master):
        self.user_entry = StringVar()
        self.change = StringVar()

        self.label = Label(master, textvariable=self.change)
        self.entry = Entry(master, textvaribale=self.user_entry)
        self.button = Button(master, text="Buttton", command=self.doSomething 

    def doSomething(self):
        self.change.set("Text Changed")
        got_it = self.user_entry.get()

root = Tk()
app = App(root)
root.mainloop()

This is described in more detail here

You should also read the official python style guide (as @jonrsharpe said), especially the part about spacing. I've corrected your spacing in my example above.

BrtH
  • 2,610
  • 16
  • 27
  • Although the example you link to defines `root` outside the class, that isn't mandatory. You could also subclass `App` from `Tk` or `Frame`. – jonrsharpe Nov 29 '14 at 16:03