我在一个类(StartPage)中有一个条目小部件,我必须在其中输入数字,输入保存在一个通用类(Variabili)中,该类可以从另一个类(FirstPage)访问。根据这个数字,我必须说出n个标签(其中n是我在上一页中输入的数字)。为此,我使用了for循环:for i in range(0, int(number))
。问题是:
我一直收到这个错误:TypeError: IntVar() missing 1 required positional argument: 'name'
下面是代码:
import tkinter as tk # python 3
from tkinter import font as tkfont
from typing_extensions import IntVar # python 3
class SampleApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.title_font = tkfont.Font(family='Helvetica', size=18, weight="bold", slant="italic")
# the container is where we'll stack a bunch of frames
# on top of each other, then the one we want visible
# will be raised above the others
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 = {}
for F in (StartPage, PageOne):
page_name = F.__name__
frame = F(parent=container, controller=self)
self.frames[page_name] = frame
# put all of the pages in the same location;
# the one on the top of the stacking order
# will be the one that is visible.
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame("StartPage")
def show_frame(self, page_name, *args):
'''Show a frame for the given page name'''
frame = self.frames[page_name]
frame.tkraise()
def get_page(self, classname):
'''Returns an instance of a page given it's class name as a string'''
for page in self.frames.values():
if str(page.__class__.__name__) == classname:
return page
return None
class Variabili:
numero_finanziatori = tk.IntVar()
class StartPage(tk.Frame, Variabili):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
#label = tk.Label(self, text="Start Page: insert value")
#label.pack(side="top", fill="x", pady=10)
#self.some_input = tk.IntVar()
#self.some_entry = tk.Entry(self, width=8, textvariable=self.some_input).pack()
self.some_input = tk.StringVar()
self.some_entry = tk.Entry(self, width=8, textvariable=Variabili.numero_finanziatori)
self.some_entry.insert(0, "1")
self.some_entry.pack()
Variabili.numero_finanziatori = self.some_entry.get()
button1 = tk.Button(self, text='Next Page', command=lambda: controller.show_frame("PageOne"))
button1.pack()
class PageOne(tk.Frame, Variabili):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
labels_finanziatori = {
'principale': [],
'capitale_investito': [],
'tasso': [],
'tempo': []
}
dict_finanziatori = {
'numero': [],
'capitale_investito': [],
'tasso': [],
'tempo': [],
}
Finanziatore_Semplice = []
k = 0
for i in range(0, int(Variabili.numero_finanziatori)):
#MAIN label del finanziatore
labels_finanziatori['principale'].append(tk.Label(self, text="Dati finanziatore numero "+str(i+1) +":\n"))
labels_finanziatori['principale'][i].grid(row=i*(k+1), column = 0)
k = k+1
button = tk.Button(self, text="Go to the start page",
command=lambda: controller.show_frame("StartPage")).grid()
if __name__ == "__main__":
app = SampleApp()
app.mainloop()
这是我得到的回溯:
File "c:/Users/trevi/Documents/WorkInProgress/Python/V0_3/backbone4.py", line 50, in <module>
class Variabili:
File "c:/Users/trevi/Documents/WorkInProgress/Python/V0_3/backbone4.py", line 51, in Variabili
numero_finanziatori = tk.IntVar()
File "C:\Users\trevi\Anaconda\Anaconda3\lib\tkinter\__init__.py", line 502, in __init__
Variable.__init__(self, master, value, name)
File "C:\Users\trevi\Anaconda\Anaconda3\lib\tkinter\__init__.py", line 317, in __init__
self._root = master._root()
AttributeError: 'NoneType' object has no attribute '_root'
IntVar
继承自Variable class输出:
关键是调用IntVar()需要一个主窗口(或根窗口)
从
Variable class
开始:此错误的原因是调用IntVar时找不到默认根。 如果
root
已在脚本中声明,则通常不将其指定为IntVar参数输出:
脚本中
root = tkinter.Tk()
的类似物是app = SampleApp()
。这就是为什么这些变量在与SampleApp关联的类中工作但是问题不是这个错误,而是PageOne类中的标签是根据在StartPage类的条目中输入的值形成的
在应用程序中,所有帧都在SampleApp类的
__init__
方法中初始化。这样,您可以通过控制器(self.controller.frames[page_name]
)访问帧在本例中,我从一个类操作另一个类的元素。还有other options
相关问题 更多 >
编程相关推荐