我正在尝试将值输入到类中设置的tkinter Entry
小部件中。当窗口小部件不在类中时,我可以使用attribute.get()
来检索值,但我不确定如何在类中调用它。
我收到的错误是:
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'
包含小部件的类名为StartPage
,小部件名为e_xf
,使用.get()
的小部件名为Enter_Inputs
提前感谢您的任何帮助,我在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()
答案 0 :(得分:0)
当您将自己传递给To_Print
方法并希望自己调用e_xf
字段时,您应该将e_xf
声明为该类的数据成员。
只需更改行
即可self.e_xf = tk.Entry(self)
self.e_xf.grid(row=1, column=1)
由此e_xf
是实例成员,您应该可以从Enter_Inputs
函数调用它。