Reputation: 191
I need access to information from my class "makeEntry" precisely textvariables. I tried make get function but i read that in python it's not necessary.
def temp():
print(e_full_name.get_text())
class makeEnetry:
def __init__(self, i_parent, i_width, i_row, i_column, i_text):
test.set(i_text)
test = StringVar()
entry = Entry(master = i_parent, width = i_width, textvariable = test)
entry.grid(row = i_row, column = i_column, padx = 5, pady =5 )
def get_text(self):
return self.test.get()
I tried to move my test = StringVar() above function, just in class to refer to it in another function but it doesn't work. I'm newbie in python and I have no idea what I have done wrong.
def temp():
print(e_full_name.get_text())
class makeEnetry:
test = StringVar()
def __init__(self, i_parent, i_width, i_row, i_column, i_text):
test.set(i_text)
.
.
.
Upvotes: 1
Views: 586
Reputation: 385820
Any variable that you want external functions to have access to need to be a instance variable.
For example, if you do self.test = StringVar()
, and use self.test
everywhere inside the class instead of just test
, then any code that has a reference to the instance of the class can access the variable.
the_entry = makeEnetry(root, 20, 1, 1, "Hello, world!")
...
print(the_entry.test.get())
However, there's nothing wrong with creating a getter or setter. For example, StringVar
has a getter (.get()
) and setter (.set(...)
). The advantage of creating getters and setters is that it separates the internal implementation from the external interface. With a getter or setter, you can hide the fact that you're using a StringVar
, the caller doesn't need to know.
The thing with functions, instance variables, and with getters and setters, is that you are creating a user interface or API. You are creating an object and then telling the outside world "this is how you interact with this object". Sometimes getters and setters make sense, sometimes they don't. It depends on what you want the object UI/API to be, and whether or not you want to expose internal implementation details to the outside world.
In this specific case it seems like the_entry.get_text()
is a cleaner interface than the_entry.test.get(...)
since it gives you more freedom in changing the internal implementation without affecting code that uses this object. It all boils down to how you want external code to interact with the object you've created. Should the caller know that this object uses a StringVar
, or should it just know that it can call get_text
to interact with it?
Upvotes: 0
Reputation: 5319
I have written a working solution. As I mentioned in my comment, the test
variable should be an instance variable (self
). I have added several comments to my example code for the better understanding.
Code:
import tkinter as tk
def temp():
print(entry_inst.get_text()) # Printing the result of "get_text" method of "entry_inst" object.
root = tk.Tk()
class makeEnetry:
def __init__(self, i_parent, i_width, i_row, i_column, i_text):
self.test = tk.StringVar()
self.test.set(i_text)
entry = tk.Entry(master=i_parent, width=i_width, textvariable=self.test)
entry.grid(row=i_row, column=i_column, padx=5, pady=5)
def get_text(self):
return self.test.get()
entry_inst = makeEnetry(root, 20, 1, 1, "TEST") # Creating an object from "makeEnetry" class
button = tk.Button(root, text="PUSH ME", command=temp) # Creating a button which will call the "temp" function
button.grid(row=2, column=1)
tk.mainloop()
GUI:
Console output:
FYI: I have changed the text in the entry box and clicked to button.
>>> python3 test.py
TEST
TEST1
TEST12
TEST123
Upvotes: 1