如何将项目添加到列表框?

时间:2017-04-01 08:05:16

标签: python python-3.x tkinter listbox

我正在创建一个简单的GUI程序来管理优先级。我在向列表框添加项目时遇到了麻烦。我尝试通过将两个属性传递给构造函数来创建Priority类的实例,然后使用 g.listBox.insert(END,item),但似乎它并没有像那样工作。我收到一个错误:

  

/usr/bin/python3.5 /home/cali/PycharmProjects/priorities/priorities.py   Tkinter回调中的异常回溯(最近一次调用最后一次):
  文件" /usr/lib/python3.5/tkinter/ init .py",第1553行,致电       return self.func(* args)File" /home/cali/PycharmProjects/priorities/priorities.py" ;,第52行,in   新增项目       item = Priority(subject = g.textBox.get(" 1.0",' end-1c'),priority = g.textBox.get(" 1.0&#34) ;,' end-1c'))AttributeError:' GuiPart'对象没有属性' textBox'

     

处理完成,退出代码为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。

2 个答案:

答案 0 :(得分:1)

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

您的代码需要考虑很多事情。如果我一个接一个地修理和评论它们,我相信我的答案会很长,而今天我会感到很懒惰。

我认为下面的程序很容易理解(另请注明澄清)。我没有找到在文本区域中如何输入适当信息所固有的规范。因此,我的程序在假定用户在文本区域的第一行键入优先主题,然后使用新行键入优先级顺序的情况下工作。单击“添加”按钮将导致在文本框小部件的同一行上插入这两个数据,如下所示:

enter image description here

这是一个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提供良好的用户体验,那么如果您添加一个按钮以允许用户清除文本区域的内容以便他可以输入新的优先级,那就太好了:

enter image description here

为此,您可以通过添加以下两行代码向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)

答案 1 :(得分:0)


这些行导致错误:

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