[英]Python Tkinter: Delete all contents from ListBox
Hey guys pretty sure this is a simple fix, but I can't find anything. 大家很确定这是一个简单的修复程序,但是我什么也找不到。 Basically I'm filling a listbox, and I need it to update (pseudo update) whenever a user clicks a label in the listbox.
基本上,我正在填充一个列表框,并且每当用户单击列表框中的标签时,我都需要它进行更新(伪更新)。
My only problem is deleting all contents from the listbox. 我唯一的问题是从列表框中删除所有内容。 Below I have the function I implemented.
下面是我实现的功能。 Thank you in advance!
先感谢您!
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
I'm surprised it's not crashing. 我很惊讶它没有崩溃。 I don't see your import statements, but contextually in your code you may have to write:
我看不到您的导入语句,但是在上下文中,您可能必须编写以下代码:
self.usersListBox.delete(0, tk.END)
I think you imported Tkinter as tk, and END is a component of tk. 我认为您将Tkinter导入为tk,而END是tk的组成部分。 Because it's not defined as such, is it possible that END is a variable in some wildcard-ed library or somewhere else in your code?
因为不是这样定义的,所以END是否可能是某些通配符库中或代码中其他地方的变量?
Specify tk.END
or "end"
in your delete function as in the example above and that would prevent this error. tk.END
例所示,在删除功能中指定tk.END
或"end"
,这样可以避免出现此错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.