I'm trying to get a value entered into a tkinter Entry
widget which is set in a class. I can use the attribute.get()
to retrieve the value when the widget isn't in a class but I'm not sure how to call it when it is in a class.
The error i receive is:
File "C:/Users/ABour/Python Scripts/test/PassVarSX.py", line 12, in Enter_Inputs
xf_In = int(self.e_xf.get())
AttributeError: 'StartPage' object has no attribute 'e_xf'
The class containing the widget is called StartPage
and the widget is called e_xf
, the function using .get()
is called Enter_Inputs
Thank you in advance for any help, I'm running this on Spyder, Python v3.6
import tkinter as tk
from tkinter import ttk
LARGE_FONT= ("Verdana", 12)
def To_Print(self):
xf = Enter_Inputs(self,'xf')
print('xf = ', xf)
def Enter_Inputs(self,x): # Enter inputs from values typed in
xf_In = int(self.e_xf.get())
if x == 'xf':
x = float(xf_In)/100
return x
class TestApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
container.pack(side="top", fill="both", expand = True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
frame = StartPage(container, self)
self.frames[StartPage] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
label = ttk.Label(self, text="Start Page", font=LARGE_FONT)
label.grid(row=0, columnspan = 2)
l_xf = tk.Label(self, text="% xA of Feed")
l_xf.grid(row=1)
e_xf = tk.Entry(self)
e_xf.grid(row=1, column=1)
b_run = tk.Button(self, text="Click to Run", command=lambda: To_Print(self))
b_run.grid(row=2, column=0, columnspan = 2)
app = TestApp()
app.mainloop()
As you pass self to To_Print
method and you want from self to call the e_xf
field, you should declare e_xf
as a data member of the class.
You can do it by simply change the line
self.e_xf = tk.Entry(self)
self.e_xf.grid(row=1, column=1)
By that the e_xf
is an instance member, and you should be able to call it from Enter_Inputs
function.