【发布时间】:2017-08-26 12:48:40
【问题描述】:
我正在创建一个简单的 GUI 程序来管理优先级。我在向列表框中添加项目时遇到问题。我尝试通过将两个属性传递给构造函数然后使用 g.listBox.insert(END, item) 来创建 Priority 类的实例,但它似乎不像那样工作。我收到一个错误:
/usr/bin/python3.5 /home/cali/PycharmProjects/priorities/priorities.py Tkinter 回调 Traceback 中的异常(最近一次调用最后一次):
call 中的文件“/usr/lib/python3.5/tkinter/init.py”,第 1553 行 返回 self.func(*args) 文件“/home/cali/PycharmProjects/priorities/priorities.py”,第 52 行,在 添加项目 item = Priority(subject = g.textBox.get("1.0", 'end-1c'), priority = g.textBox.get("1.0", 'end-1c')) AttributeError: 'GuiPart' 对象没有属性“文本框”进程以退出代码 0 结束
这是我所做的:
# priorities.py
# GUI program to manage priorities
from tkinter import *
class Priority:
def __init__(self, subject, priority):
self.subject = subject
self.priority = priority
def subject(self):
return self.subject
def priority(self):
return self.priority
class GuiPart:
def __init__(self):
self.root = self.createWindow()
def createWindow(self):
root = Tk()
root.resizable(width = False, height = False)
root.title("Priorities")
return root
def createWidgets(self):
Button(self.root,
text = "Add",
command = self.addItem).grid(row = 2, column = 0, sticky = W + E)
Button(self.root,
text="Remove",
command = self.removeItem).grid(row = 2, column = 1, sticky = W + E)
Button(self.root,
text="Edit",
command = self.editItem).grid(row = 2, column = 2, sticky = W + E)
listBox = Listbox(width = 30).grid(row = 1, sticky = W + E, columnspan = 3)
textBox = Text(height=10, width=30).grid(row = 3, columnspan = 3, sticky = W + E + N + S)
def addItem(self):
item = Priority(subject = g.textBox.get("1.0", 'end-1c'), priority = g.textBox.get("1.0", 'end-1c'))
g.listBox.insert(END, item)
def removeItem(self):
pass
def editItem(self):
pass
class Client:
pass
if __name__ == "__main__":
g = GuiPart()
g.createWidgets()
g.root.mainloop()
我使用的是 Python 3.5。
【问题讨论】:
标签: python python-3.x tkinter listbox