Python - 从单独的 class 中的 tk 输入框检索值
Python - retrieving value from tk entry box in separate class
我正在尝试将一个值输入到 class 中设置的 tkinter Entry
小部件中。当小部件不在 class 中时,我可以使用 attribute.get()
检索值,但我不确定当它在 class.[=20 中时如何调用它=]
我收到的错误是:
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'
包含widget的class调用StartPage
,widget调用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()
当您将 self 传递给 To_Print
方法并希望从 self 调用 e_xf
字段时,您应该将 e_xf
声明为 class 的数据成员.
你可以通过简单地改变行来做到这一点
self.e_xf = tk.Entry(self)
self.e_xf.grid(row=1, column=1)
因为 e_xf
是一个实例成员,您应该可以从 Enter_Inputs
函数中调用它。
我正在尝试将一个值输入到 class 中设置的 tkinter Entry
小部件中。当小部件不在 class 中时,我可以使用 attribute.get()
检索值,但我不确定当它在 class.[=20 中时如何调用它=]
我收到的错误是:
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'
包含widget的class调用StartPage
,widget调用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()
当您将 self 传递给 To_Print
方法并希望从 self 调用 e_xf
字段时,您应该将 e_xf
声明为 class 的数据成员.
你可以通过简单地改变行来做到这一点
self.e_xf = tk.Entry(self)
self.e_xf.grid(row=1, column=1)
因为 e_xf
是一个实例成员,您应该可以从 Enter_Inputs
函数中调用它。