如何使用tkinter创建打字指示器?

时间:2019-03-06 18:02:37

标签: python python-3.x sockets tkinter python-multithreading

我使用tkinter在Python中建立了一个实时聊天室。

我想知道如何在不使用Firebase的情况下创建打字指示器。我的意思是说,用户名之一是bob,如果bob正在键入一条消息,我希望它向其他用户说bob is typing...并且我想要它,如果可能会出现在类型消息框中。

编辑:

我在以下位置添加了此

def var_changed(self):
   self.label.config(text=msg+"is typing")

my_msg = tkinter.StringVar()
my_msg.trace = ("w", var_changed)
entry_field = tkinter.Entry(top, textvariable=my_msg)
entry_field.pack()

但是它所要做的就是复制我正在写的内容,而不是说用户键入消息

Server.py

import socket, threading

host = socket.gethostname()
port = 4000
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host,port))
s.listen()
clients = {}
addresses = {}
print(host)
print("Server is ready...")
serverRunning = True

def handle_client(conn):
    try:
        data = conn.recv(1024).decode('utf8')
        welcome = 'Welcome %s! If you ever want to quit, type {quit} to exit.' % data
        conn.send(bytes(welcome, "utf8"))
        msg = "%s has joined the chat" % data
        broadcast(bytes(msg, "utf8"))
        clients[conn] = data
        while True:
            found = False
            response = 'Number of People Online\n'
            msg1 = conn.recv(1024)

            if msg1 != bytes("{quit}", "utf8"):
                broadcast(msg1, data+": ")
            else:
                conn.send(bytes("{quit}", "utf8"))
                conn.close()
                del clients[conn]
                broadcast(bytes("%s has left the chat." % data, "utf8"))
                break
    except:
        print("%s has left the chat." % data)

def broadcast(msg, prefix=""):
    for sock in clients:
        sock.send(bytes(prefix, "utf8")+msg)

while True:
    conn,addr = s.accept()
    conn.send("Enter username: ".encode("utf8"))
    print("%s:%s has connected." % addr)
    addresses[conn] = addr
    threading.Thread(target = handle_client, args = (conn,)).start()

Client.py

import socket,threading,tkinter

host = input("Enter server name: ")
port = 4000
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
address = (host,port)

def echo_data(sock):
   while True:
      try:
         msg = sock.recv(1024).decode('utf8')
         msg_list.insert(tkinter.END, msg)
      except OSError:
         break

def send(event=None):
   msg = my_msg.get()
   my_msg.set("")
   s.send(bytes(msg, "utf8"))
   if msg == "{quit}":
      s.close()
      top.quit()

def on_closing(event=None):
    my_msg.set("{quit}")
    send()

top = tkinter.Tk()
top.title("Chat Room")

messages_frame = tkinter.Frame(top)
my_msg = tkinter.StringVar()
my_msg.set("Type your messages here.")
scrollbar = tkinter.Scrollbar(messages_frame)
msg_list = tkinter.Listbox(messages_frame, height=15, width=100, yscrollcommand=scrollbar.set)
scrollbar.pack(side=tkinter.RIGHT, fill=tkinter.Y)
msg_list.pack(side=tkinter.LEFT, fill=tkinter.BOTH)
msg_list.pack()
messages_frame.pack()

entry_field = tkinter.Entry(top, textvariable=my_msg)
entry_field.bind("<Return>", send)
entry_field.pack()
send_button = tkinter.Button(top, text="Send", command=send)
send_button.pack()

top.protocol("WM_DELETE_WINDOW", on_closing)

address = (host,port)
s.connect(address)

threading.Thread(target=echo_data, args = (s,)).start()

tkinter.mainloop()

1 个答案:

答案 0 :(得分:2)

  

问题:为tkinter.Entry小部件创建键入指示器

  1. 定义从tk.Tk继承的应用程序类,仅用于演示。

    import tkinter as tk
    
    class App(tk.Tk):
    
        def __init__(self):
            super().__init__()
    
  2. 设置一个标志.is_typing,以知道检测到键入

            self.is_typing = False
    
  3. 创建一个常见的Entry

            self.my_msg = tk.StringVar()
            entry_field = tk.Entry(self, textvariable=self.my_msg)
            entry_field.grid(row=0, column=0)
    
  4. '<KeyRelease>'事件绑定到Entry实例,该实例对于每个键释放事件都调用函数self.typing(...

            entry_field.bind('<KeyRelease>', self.typing)
    
  5. 创建一个LabelButton仅用于演示目的

            self.label = tk.Label(self)
            self.label.grid(row=1, column=0)
    
            btn = tk.Button(self, text='Send', command=self.send_msg)
            btn.grid(row=2, column=0)
    
  6. 重置标志.is_typing并在发送消息时清除Label文本,仅供演示。

        def send_msg(self):
            self.is_typing = False
            self.label.config(text="")
    
  7. 在每次按键释放事件中都会调用的函数def typing(...
    如果设置了标志.is_typing,则无需进一步操作即可返回。
    如果未设置标志,则为第一个呼叫:

    • 设置标志
    • 执行操作,她只将Label文本设置为"is typing"

      def typing(self, *args):
          if self.is_typing:
              return
      
          self.is_typing = True
          self.label.config(text="is typing")
      
  8. 运行演示应用

    if __name__ == "__main__":
        App().mainloop()
    

使用Python测试:3.5