from tkinter import *
window = Tk()
ia_answers= "trolol"
input_frame = LabelFrame(window, text="User :", borderwidth=4)
input_frame.pack(fill=BOTH, side=BOTTOM)
input_user = StringVar()
input_field = Entry(input_frame, text=input_user)
input_field.pack(fill=BOTH, side=BOTTOM)
ia_frame = LabelFrame(window, text="Discussion",borderwidth = 15, height = 100, width = 100)
ia_frame.pack(fill=BOTH, side=TOP)
user_says = StringVar()
user_text = Label(ia_frame, textvariable=user_says, anchor = NE, justify = RIGHT, bg="white")
user_text.pack(fill=BOTH, side=TOP)
ia_says = StringVar()
ia_text = Label(ia_frame, textvariable=ia_says, anchor = W, justify = LEFT, bg="white")
ia_text.pack(fill=BOTH, side=BOTTOM)
def Enter_pressed(event):
"""Took the current string in the Entry field."""
input_get = input_field.get()
input_user.set("")
user_says.set(input_get + "\n\n")
ia_says.set(ia_answers)
input_field.bind("<Return>", Enter_pressed)
window.mainloop()
嗨,我正在尝试建立一个讨论机器人。你知道吗
当我执行代码时,输入字段中的问题和答案会正确显示。你知道吗
问题是在输入下一句话后,上一个问题/答案将被删除。举个例子:
Hello Bot
Hello User
(then the text disappears)
How are you
Fine thank you
我想要的是:
Hello Bot
Hello User
(then the text stays in the frame)
How are you
Fine thank you
问题发生在-
您将替换
users_says.set()
,ia_says.set()
并用新值重置完整的标签。相反,您应该获取旧值并将新值附加到它,然后将其设置回原来的值,例如-或者您也可以为每个新事件创建一个新标签,并将其添加到
LabelFrame
。示例-相关问题 更多 >
编程相关推荐