如何解决为什么当我使用 time.sleep(0.5) 而不是只有 1 个函数时,everythin 会休眠 0.5 秒?
我正在使用 tkinter 在 python 中制作一个响应机器人。为此,当用户输入某些内容时,我会给出答案。 我还没有完成。我希望机器人应该在一段时间后回答,这样看起来非常好。
我的代码:-
import tkinter
from tkinter import Message,messageBox
from tkinter import font
from tkinter.constants import CENTER,LEFT,RIGHT,BottOM,TOP
from tkinter import scrolledtext
from typing import Sized
import time
window = tkinter.Tk()
window.geometry("370x500")
window.configure(bg="orange")
#variables
running = True
verdana_12 = ('Verdana','12')
verdana_10 = ('Verdana','10')
verdana_9 = ('Verdana','9')
msg=tkinter.StringVar()
#messages
greetings = ["hi","hello","hey","what's up!"]
questions = [
' 1. What is python?',' 2. Where to ask questions if I get stuck?',' 3. How can I get example questions and quizes related to python?'
]
#items inb gui
info_text = tkinter.Label(window,text="Chat",bg="orange",font=verdana_12)
info_text.pack(padx=20,pady=5)
text_1 = tkinter.Label(window,text="Type your message: ",font=verdana_9,bg="orange")
text_1.place(x=0,y=476)
input_area = tkinter.Entry(window,justify=LEFT,textvariable=msg,font=verdana_12,width=16)
input_area.focus_force()
input_area.place(x=135,y=476)
chat_area = scrolledtext.ScrolledText(window)
chat_area.pack(padx=20,pady=5)
chat_area.config(state = "disabled")
#define message
message = f"You: {input_area.get()}\n"
#functions
#def afterwards():
# mine_msg = message.lower().strip().split()[1]
# if 1 == mine_msg:
# reply("Bot: Python is an interpreted high-level general-purpose programming language. Python's design philosophy emphasizes code readability with its notable use of significant indentation. More info at https://en.wikipedia.org/wiki/Python_(programming_language)")
#
# elif 2 == mine_msg:
# reply("Bot: If you get stuck in python programming,go to stackoverflow.com where you can ask questions related to any programming language!")
#
# elif 3 == mine_msg:
# reply("Bot: You can get quizes related to python in w3schools.org and example questions in w3resource.org.")
def reply(reply_msg):
str(reply_msg)
the_reply = f"{reply_msg}\n" #input_area = where person types.
chat_area.config(state='normal')
chat_area.insert(tkinter.INSERT,the_reply)
chat_area.yview('end')
chat_area.config(state='disabled')
input_area.delete(0,'end')
def check_msg():
global message
print('Message =',message.strip())
try:
mine_msg = message.lower().strip().split()[1]
if greetings[0] == mine_msg or greetings[1] == mine_msg or greetings[2] == mine_msg or greetings[3] == mine_msg:
reply("Bot: Hello,here's how I can help you")
for i in questions:
reply(i)
#afterwards()
else:
reply("Bot: Couldn't understand your message,please type 'hi','hello','hey' or 'what's up!' to get responce.")
except IndexError:
pass
def write():
global message
if len(input_area.get().split()) > 0:
message = f"You: {input_area.get()}\n" #input_area = where person types.
chat_area.config(state='normal')
chat_area.insert(tkinter.INSERT,message)
chat_area.yview('end')
chat_area.config(state='disabled')
input_area.delete(0,'end')
check_msg()
else:
reply('Please type something.')
def print_it():
message = f"You: {input_area.get()}"
print(message)
#button_send
send_button = tkinter.Button(window,text="Send",command=write,font=verdana_10,bg="gray",fg="white",width=26,height=1)
send_button.pack(padx=20,pady=5)
window.mainloop()
它冻结了 0.5 的所有内容,而不是我希望它只冻结 write() 函数。请提供任何帮助。
谢谢!
解决方法
实现此目的的一种方法是使用 tkinter 的 .after 方法将消息回复延迟一段时间。
i = "My Message"
window.after(1000,lambda i=i: reply(i))
这将在 1000 毫秒或 1 秒后调用带有消息“我的消息”的回复函数。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。