与tkin讨论

2024-06-17 13:10:07 发布

您现在位置:Python中文网/ 问答频道 /正文

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

Tags: thetextyoufieldhelloinputwindowfill
1条回答
网友
1楼 · 发布于 2024-06-17 13:10:07

问题发生在-

user_says.set(input_get + "\n\n")
ia_says.set(ia_answers)

您将替换users_says.set()ia_says.set()并用新值重置完整的标签。相反,您应该获取旧值并将新值附加到它,然后将其设置回原来的值,例如-

user_says.set(user_says.get() + input_get + "\n")
ia_says.set(ia_says.get() + ia_answers + "\n")

或者您也可以为每个新事件创建一个新标签,并将其添加到LabelFrame。示例-

from tkinter import *

window = Tk()

ia_answers= "trolol\n"
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=X) 

ia_says = StringVar()
ia_text = Label(ia_frame, textvariable=ia_says, anchor = NW, justify = LEFT, bg="white") 
ia_text.pack(fill=X) 

user_texts = []
ia_texts = []
user_says_list = []
ia_says_list = []
def Enter_pressed(event):
    """Took the current string in the Entry field."""
    input_get = input_field.get()
    input_user.set("")
    user_says1 = StringVar()
    user_says1.set(input_get + "\n")
    user_text1 = Label(ia_frame, textvariable=user_says1, anchor = NE, justify =   RIGHT, 

bg="white") 
    user_text1.pack(fill=X)
    user_texts.append(user_text1)
    user_says_list.append(user_says1)
    ia_says1 = StringVar()
    ia_says1.set(ia_answers)
    ia_text1 = Label(ia_frame, textvariable=ia_says1, anchor = NW, justify = LEFT, 

bg="white") 
    ia_text1.pack(fill=X)
    ia_texts.append(ia_text1)
    ia_says_list.append(ia_says1)

input_field.bind("<Return>", Enter_pressed)
window.mainloop()

相关问题 更多 >