[英]tkinter can't convert entry string to int
我在類 (StartPage) 中有一個條目小部件,我必須在其中輸入數字,輸入保存在可從另一個類 (FirstPage) 訪問的通用類 (Variabili) 中。 基於該數字,我必須對 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 類。
from inspect import signature
import tkinter
print("\n----- IntVar Method Resolution Order -----\n")
print(tkinter.IntVar.mro())
print("\n----- __init__ signature -----\n")
print("Variable:", signature(tkinter.Variable.__init__))
print("IntVar:", signature(tkinter.IntVar.__init__))
輸出:
----- IntVar Method Resolution Order -----
[<class 'tkinter.IntVar'>, <class 'tkinter.Variable'>, <class 'object'>]
----- __init__ signature -----
Variable: (self, master=None, value=None, name=None)
IntVar: (self, master=None, value=None, name=None)
關鍵是調用IntVar()需要一個主窗口(或根窗口)。
從Variable class
:
if not master:
master = _get_default_root('create variable')
self._root = master._root()
self._tk = master.tk
這個錯誤的原因是調用 IntVar 時沒有找到默認的根。 如果root
已在腳本中聲明,則通常不會將其指定為 IntVar 參數。
import tkinter
print("\n----- root -----\n")
root = tkinter.Tk()
print(root.tk)
print("is _default_root:", tkinter._default_root is root)
print("\n----- IntVar -----\n")
var = tkinter.IntVar()
print(var._tk)
輸出:
----- root -----
<_tkinter.tkapp object at 0x00000080E160E7B0>
is _default_root: True
----- IntVar -----
<_tkinter.tkapp object at 0x00000080E160E7B0>
腳本中root = tkinter.Tk()
的類似物是app = SampleApp()
。 這就是這些變量在與 SampleApp 關聯的類中起作用的原因。
但問題不在於這個錯誤,而在於PageOne 類中的標簽是根據 StartPage 類中的 Entry 中輸入的值形成的。
在您的應用程序中,所有幀都在 SampleApp 類的__init__
方法中初始化。 這樣您就可以通過控制器( self.controller.frames[page_name]
)訪問框架。
在這個例子中,我從另一個類操作一個類的元素。 還有其他選擇。
import tkinter as tk # python 3
from tkinter import font as tkfont
#from typing_extensions import IntVar # python 3
class SampleApp(tk.Tk):
# same
...
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
# TypeError is thrown for self.some_input.get() if the value entered is not a number
# see: https://stackoverflow.com/questions/4140437/interactively-validating-entry-widget-content-in-tkinter
# https://anzeljg.github.io/rin2/book2/2405/docs/tkinter/entry-validation.html
self.some_input = tk.IntVar()
# set the default to 1
self.some_input.set(1)
self.some_entry = tk.Entry(self, width=8, textvariable=self.some_input)
self.some_entry.pack()
# button to open the next page with certain conditions
button1 = tk.Button(self, text='Next Page', command=self.show_next_frame)
button1.pack()
def show_next_frame(self):
if self.some_input.get() == 0:
pass
else:
# create labels in the PageOne class using the entered value
self.controller.frames["PageOne"].create_labels(self.some_input.get())
# reset the default to 1
self.some_input.set(1)
# show the prepared page
self.controller.show_frame("PageOne")
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
self.labels_finanziatori = {
'principale': [],
'capitale_investito': [],
'tasso': [],
'tempo': []
}
dict_finanziatori = {
'numero': [],
'capitale_investito': [],
'tasso': [],
'tempo': [],
}
Finanziatore_Semplice = []
def create_labels(self, some_input):
k = 0
for i in range(0, some_input):
# create new labels
label = tk.Label(self, text="Dati finanziatore numero "+str(i+1) +":\n")
label.grid(row=i*(k+1), column = 0)
# you can save labels if needed
self.labels_finanziatori['principale'].append(label)
k = k+1
# button on the side of the labels
button = tk.Button(self, text="Go to the start page",
command=lambda: self.controller.show_frame("StartPage")).grid(row=0, column=1)
if __name__ == "__main__":
app = SampleApp()
app.mainloop()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.