【问题标题】:How to add item to listbox?如何将项目添加到列表框?
【发布时间】: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


    【解决方案1】:

    因此,如果我理解您的目标,您将尝试通过允许用户在文本区域小部件中键入其主题和顺序的信息来描述优先级;之后,用户可以点击“添加”按钮将优先级信息插入到您的列表框中。

    围绕您的代码有很多事情需要考虑。如果我去修复和评论它们,我相信我的答案会很长,而我今天很懒。

    我认为我下面的程序很容易理解(否则要求澄清)。我没有找到有关如何在文本区域中键入专有信息的规范。所以我下面的程序假设用户在文本区域的第一行输入优先主题,然后使用新行输入优先顺序。点击“添加”按钮将导致这2个数据插入到文本框小部件的同一行,如下所示:

    这是一个 MCVE:

    import tkinter as tk
    
    class ProioritiesManager(tk.Frame):
    
       def __init__(self, master):
           tk.Frame.__init__(self, master)
           self.master = master
           self.master.resizable(width = False, height = False)
           self.master.title("Priorities")
           self.create_buttons()
           self.create_listbox()
           self.create_priorities_description_zone()
    
       def create_buttons(self):
           add_item = tk.Button(self.master, text='Add', command=self.add_item)
           add_item.grid(row=2, column=0, sticky=tk.W+tk.E)
           remove_item = tk.Button(self.master, text='Remove', command=self.remove_item)
           remove_item.grid(row=2, column=1, sticky=tk.W+tk.E)
           edit_item = tk.Button(self.master, text='Edit', command=self.edit_item)
           edit_item.grid(row=2, column=2, sticky=tk.W+tk.E)
    
       def create_listbox(self):
           self.item_alternatives = tk.Listbox(self.master, width=30)
           self.item_alternatives.grid(row=1, sticky=tk.W+tk.E, columnspan=3)   
    
       def create_priorities_description_zone(self):       
           self.priority_text_zone = tk.Text(self.master, height=10, width=30)
           self.priority_text_zone.grid(row=3, columnspan=3, sticky=tk.W+tk.E+tk.N+tk.S)
    
       def get_priority_subject(self):
           return self.priority_text_zone.get('1.0', '1.0 lineend')
    
       def get_priority_order(self):
           return self.priority_text_zone.get('2.0', '2.0 lineend')  
    
       def add_item(self):
           self.item_alternatives.insert(tk.END, self.get_priority_subject()+'  '+ self.get_priority_order())
    
       def remove_item(self):
           pass
    
       def edit_item(self):
           pass
    
    
    
    if __name__ == "__main__":
        root = tk.Tk()
        ProioritiesManager(root)
        root.mainloop()
    

    如果您想为您的 GUI 提供良好的用户体验,那么最好添加一个按钮以允许用户清除文本区域的内容,以便他可以输入新的优先级:

    为此,您可以通过添加这两行代码来为create_buttons()函数添加一个休息按钮:

    clear_text_area = tk.Button(self.master, text='Reset', command=self.reset_priority_text_zone)
    clear_text_area.grid(row=4, column=2)
    

    回调reset_priority_text_zone() 是这样定义的:

    def reset_priority_text_zone(self):
           self.priority_text_zone.delete('1.0', tk.END)
    

    【讨论】:

    • 不客气,如果我的代码中有你不明白的地方,请告诉我。
    【解决方案2】:


    这些行导致错误:

    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)
    

    这样做:

    self.listBox = Listbox(self.root,width = 30)
    self.listBox.grid(row = 1, sticky = W + E, columnspan = 3)
    self.textBox = Text(self.root,height=10, width=30)
    self.textBox.grid(row = 3, columnspan = 3, sticky = W + E + N + S)
    


    实际上,您不是在创建 listBox 和 textBox 对象,而是 grid 正在返回到 listBox 和 textBox

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-01
      • 2015-12-11
      • 1970-01-01
      • 2017-07-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多