Python Tkinter:从ListBox中删除所有内容

时间:2018-03-26 17:08:00

标签: python tkinter listbox

嘿伙计们很确定这是一个简单的解决办法,但我找不到任何东西。基本上我填写了一个列表框,每当用户点击列表框中的标签时我都需要它来更新(伪更新)。

我唯一的问题是从列表框中删除所有内容。下面我有我实现的功能。提前谢谢!

class SocketThreadedTask(threading.Thread):
def _init_(self, socket, callback):
    threading.Thread._init_(self)
    self.socket = socket
    self.callback = callback
    self.allChannels = None
    self.privateChannels = {}
    self.publicChannels = {}
    self.channelMessages = {}

def run(self):
    channels = None
    messages = None
    while True:
        try:
            message = self.socket.receive()
            if message[0] == "channels":
                self.allChannels = message[1]
                for key, value in self.allChannels.items():
                    if key[0] == '+':
                        oldKey = key
                        newKey = key[1:]
                        self.allChannels[newKey] = value
                        del self.allChannels[oldKey]
                        self.publicChannels[key] = value
                    elif key[0] == '-':
                        key = key[1:]
                        self.privateChannels[key] = value
                message = message[0]
                self.callback(None, self.allChannels, None)
            elif message[0] == "messages":
                self.channelMessages = messages = message[1]
                message = message[0]
                self.callback(message, None, messages)
            else:
                message = message[0]
                self.callback(message, None, None)
            if message == '/quit':
                self.callback('> You have been disconnected from the chat room.')
                self.socket.disconnect()
                break
        except OSError:
            break

class ChatWindow(tk.Frame):
def _init_(self, parent):
    tk.Frame._init_(self, parent)

    self.initUI(parent)

def initUI(self, parent):
    self.messageScrollbar = tk.Scrollbar(parent, orient=tk.VERTICAL)
    self.messageScrollbar.grid(row=0, column=3, sticky="ns")

    self.messageTextArea = tk.Text(parent, bg="white", state=tk.DISABLED, yscrollcommand=self.messageScrollbar.set, wrap=tk.WORD)
    self.messageTextArea.grid(row=0, column=0, columnspan=2, sticky="nsew")

    # list of users
    self.usersListBox = tk.Listbox(parent, bg="white")
    self.usersListBox.grid(row=0, column=4, padx=5, sticky="nsew")

    self.entryField = entry.BaseEntry(parent, placeholder="Enter message.", width=80)
    self.entryField.grid(row=1, column=0, padx=5, pady=10, sticky="we")

    self.send_message_button = tk.Button(parent, text="Send", width=10, bg="#CACACA", activebackground="#CACACA")
    self.send_message_button.grid(row=1, column=1, padx=5, sticky="we")

def update_window(self, message, channels, messages):
    if (message is None) and (messages is None):
        self.refresh_users(channels)
    elif (channels is None) and (message is None):
        self.refresh_messages(messages)
    elif (channels is None) and (messages is None):
        self.update_chat_window(message)

def refresh_users(self, channels = {}):
     self.usersListBox.delete(0, END)
     self.usersListBox.insert(0, "Channels")
     if bool(channels) is True:
          count = 1
          for key, value in channels.items():
               label = Label(self.userListBox, text=key)
               label.pack()
               label.bind(None, self.switch(label.cget("text")))
               self.usersListBox.insert(count, label)
               count += 1

This is before I message, on the first update.

我会附上2张图片When anyone on the channel speaks, it's supposed to update the listbox.

1 个答案:

答案 0 :(得分:0)

我很惊讶它没有崩溃。我看不到您的导入语句,但是在上下文中,您可能需要编写以下代码:

 self.usersListBox.delete(0, tk.END)

我认为您将Tkinter导入为tk,而END是tk的组成部分。因为不是这样定义的,所以在某些通配符库或代码中的其他地方,END是否可能是变量?

如上例所示,在删除函数中指定tk.END"end",这样可以防止出现此错误。