【发布时间】:2015-08-05 19:09:40
【问题描述】:
我正在尝试构建一个回答用户问题的机器人,我想在框架的右侧显示用户问题,在左侧显示机器人答案。我已经阅读了一篇关于使用标签 (How to set justification on Tkinter Text box) 在文本中进行对齐的帖子,但我无法将其应用于我的代码,而且我根本不熟悉标签。你能帮我吗,我做错了什么? (如果不清楚请告诉我)
这是我的代码:
from tkinter import *
window = Tk()
ia_answers= "test\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)
text = Text(ia_frame, state='disabled', text="ok")
text.pack()
text.tag_configure("right", justify='right')
text.tag_add("right", 1.0, "end")
def Enter_pressed(event):
"""Took the current string in the Entry field."""
input_get = input_field.get()
input_user.set("")
text.configure(state='normal')
text.insert('end', input_get)
text.insert('end',ia_answers)
text.configure(state='disabled')
input_field.bind("<Return>", Enter_pressed)
window.mainloop()
【问题讨论】:
-
你为什么不简单地使用 2 个文本小部件?
-
我已经尝试过,但它会说我必须为每个问题和答案创建一个文本小部件,以便有类似:问题答案问题答案而不是问题问题答案答案