当我尝试在 Tkinter 中更新 Progressbar 的值属性时,为什么会出现类型错误?
Why do I get a type error when I attempt to update the value attribute of Progressbar in Tkinter?
我正在使用 Python 3.8.2
我有以下具有 3 个主循环的代码。我需要一个简单的 UI 来显示每个循环的进度。
import tkinter as tk
from tkinter import filedialog, ttk
from tkinter.ttk import Progressbar
import json
class App(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.filename = ''
tk.Button(self, text = 'Upload File', command = self.get_file).grid(row = 0, column = 0)
tk.Button(self, text = 'Verify', command = self.verify).grid(row = 1, column = 0)
# tk.Label(self, text = 'some name').grid(row = 0, column = 1)
self.mainloop()
def get_file(self):
self.filename = tk.filedialog.askopenfilename(title = "Select File", filetypes = [("JSON", "*.json")])
tk.Label(self, text = self.filename).grid(row = 0, column = 1)
def verify(self):
tk.Label(self, text = 'Verifying Trustee Public Keys').grid(row = 2, column = 0)
ttk.Progressbar(self,
length = 100,
orient = "horizontal",
maximum = len(self.data["trustee_public_keys"]),
value = 0).grid(row = 3, column = 0)
i = 1
for trustee_key_list in self.data['trustee_public_keys']:
print('Trustee :', i)
//some code
i = i+1
ttk.Progressbar['value'] = i
每次我 运行 它都会收到错误消息:
File "C:/Users/Student User/AppData/Roaming/JetBrains/PyCharmCE2020.1/scratches/scratch_17.py", line 81, in verify
ttk.Progressbar['value'] = i
TypeError: 'type' object does not support item assignment
我哪里做错了,我怎样才能得到预期的结果?
您的代码尝试将值分配给进度条类型,而您应该将该值分配给进度条的实例。
def verify(self):
tk.Label(self, text = 'Verifying Trustee Public Keys').grid(row = 2, column = 0)
progress = ttk.Progressbar(self,
length = 100,
orient = "horizontal",
maximum = len(self.data["trustee_public_keys"]),
value = 0)
progress.grid(row = 3, column = 0)
i = 1
for trustee_key_list in self.data['trustee_public_keys']:
print('Trustee :', i)
//some code
i = i+1
progress['value'] = i
我正在使用 Python 3.8.2 我有以下具有 3 个主循环的代码。我需要一个简单的 UI 来显示每个循环的进度。
import tkinter as tk
from tkinter import filedialog, ttk
from tkinter.ttk import Progressbar
import json
class App(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.filename = ''
tk.Button(self, text = 'Upload File', command = self.get_file).grid(row = 0, column = 0)
tk.Button(self, text = 'Verify', command = self.verify).grid(row = 1, column = 0)
# tk.Label(self, text = 'some name').grid(row = 0, column = 1)
self.mainloop()
def get_file(self):
self.filename = tk.filedialog.askopenfilename(title = "Select File", filetypes = [("JSON", "*.json")])
tk.Label(self, text = self.filename).grid(row = 0, column = 1)
def verify(self):
tk.Label(self, text = 'Verifying Trustee Public Keys').grid(row = 2, column = 0)
ttk.Progressbar(self,
length = 100,
orient = "horizontal",
maximum = len(self.data["trustee_public_keys"]),
value = 0).grid(row = 3, column = 0)
i = 1
for trustee_key_list in self.data['trustee_public_keys']:
print('Trustee :', i)
//some code
i = i+1
ttk.Progressbar['value'] = i
每次我 运行 它都会收到错误消息:
File "C:/Users/Student User/AppData/Roaming/JetBrains/PyCharmCE2020.1/scratches/scratch_17.py", line 81, in verify
ttk.Progressbar['value'] = i
TypeError: 'type' object does not support item assignment
我哪里做错了,我怎样才能得到预期的结果?
您的代码尝试将值分配给进度条类型,而您应该将该值分配给进度条的实例。
def verify(self):
tk.Label(self, text = 'Verifying Trustee Public Keys').grid(row = 2, column = 0)
progress = ttk.Progressbar(self,
length = 100,
orient = "horizontal",
maximum = len(self.data["trustee_public_keys"]),
value = 0)
progress.grid(row = 3, column = 0)
i = 1
for trustee_key_list in self.data['trustee_public_keys']:
print('Trustee :', i)
//some code
i = i+1
progress['value'] = i