该程序是一个聊天机器人的界面程序,主要功能是让用户输入文本,调用聊天机器人的模型或API,返回机器人的回复,并显示在界面上。
下面从以下几个方面讲解此程序的创新和功能。文章来源地址https://www.toymoban.com/news/detail-500853.html
- GUI功能:程序使用了Python内置库
tkinter
作为GUI框架,实现了对话记录、文字输入、发送按钮和头像选择功能,具有简洁、实用和美观的特点;- 角色长相选择功能:程序提供角色长相功能,允许用户根据自己的需要选择所需的图片作为角色长相,从而增加了交互的乐趣和趣味性;
- 记录保存功能:程序提供保存聊天记录的功能,可以将聊天记录保存在本地文件中,从而方便用户查看历史记录;
- 右侧自动滚动:为了让用户能够方便地查看对话进程,程序采用了右侧自动滚动的功能,还提供了鼠标悬停停止滚动功能,增加了程序的交互性和用户友好性;
- 自选角色功能:程序提供了选择角色的功能,可以自由选择聊天时机器人的发言人角色以增加娱乐趣味性,自由在role.txt设置角色相关信息作为与自己对话的角色背景信息;
- 聊天机器人自主选择回答:
- 当用户将问题提交时,程序通过接口调用外部聊天机器人API,返回机器人的回答结果,并在界面上显示。
- 通过模型调用,程序还可以根据聊天记录,模拟用户的行为和习惯,使用机器学习或自我编程算法,自主选择回答。 该程序采用了多种创新性的设计思路和技术,使得用户可以方便、高效地与聊天机器人进行交互,在所需时刻获取所需的回答和信息。
import time
from tkinter import *
from tkinter.filedialog import askopenfilename
from tkinter.messagebox import showinfo
from tkinter.ttk import *
from typing import Dict
from PIL import ImageTk
from PIL import Image
class WinGUI(Tk):
widget_dic: Dict[str, Widget] = {}
def __init__(self):
super().__init__()
self.__win()
self.widget_dic["tk_label_image_label"] = self.__tk_label_image_label(self)
self.widget_dic["tk_text_chat_win"] = self.__tk_text_chat_win(self)
self.widget_dic["tk_text_chat_text_input"] = self.__tk_text_chat_text_input(self)
self.widget_dic["tk_button_send_button"] = self.__tk_button_send_button(self)
def __win(self):
self.title("chat")
# 设置窗口大小、居中
width = 812
height = 530
screenwidth = self.winfo_screenwidth()
screenheight = self.winfo_screenheight()
geometry = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
self.geometry(geometry)
self.resizable(width=False, height=False)
img = Image.open("2023.png")
img = img.resize((270, 530))
# send= Image.open("send.png")
# send = send.resize((270, 530))
self.widget_dic["image"] = ImageTk.PhotoImage(img)
# self.widget_dic["send"] = ImageTk.PhotoImage(send)
self.widget_dic["role"] = ""
# 自动隐藏滚动条
def scrollbar_autohide(self, bar, widget):
self.__scrollbar_hide(bar, widget)
widget.bind("<Enter>", lambda e: self.__scrollbar_show(bar, widget))
bar.bind("<Enter>", lambda e: self.__scrollbar_show(bar, widget))
widget.bind("<Leave>", lambda e: self.__scrollbar_hide(bar, widget))
bar.bind("<Leave>", lambda e: self.__scrollbar_hide(bar, widget))
def __scrollbar_show(self, bar, widget):
bar.lift(widget)
def __scrollbar_hide(self, bar, widget):
bar.lower(widget)
def __tk_label_image_label(self, parent):
# print(type(photo))
label = Label(parent, text="标签", anchor="center", )
label.place(x=0, y=0, width=270, height=530)
label.config(image=self.widget_dic["image"])
return label
def __tk_text_chat_win(self, parent):
text = Text(parent)
# text = scrolledtext.ScrolledText(parent)
try:
with open("history.txt","r",encoding="utf-8") as f:
history=f.read()
except:
history=""
if history:
text.insert(END,history)
text.place(x=280, y=0, width=525, height=439)
return text
def __tk_text_chat_text_input(self, parent):
text = Text(parent)
text.place(x=280, y=450, width=459, height=80)
return text
def __tk_button_send_button(self, parent):
btn = Button(parent, text="发送", takefocus=False )
btn.place(x=740, y=450, width=66, height=79)
return btn
class Win(WinGUI):
def __init__(self):
super().__init__()
self.__event_bind()
self.config(menu=self.create_menu())
def menu_litvkmr7(self, parent):
menu = Menu(parent, tearoff=False)
menu.add_command(label="set_player", command=self.set_player_txt)
return menu
def create_menu(self):
menu = Menu(self, tearoff=False)
menu.add_cascade(label="player", menu=self.menu_litvkmr7(menu))
return menu
def set_player_txt(self):
print("点击了菜单")
filetypes = [("txt files", "*.txt")]
# filetypes = [("PNG", "*.png"), ("JPG", "*.jpg"), ("GIF", "*.gif"), ("txt files", "*.txt"), ('All files', '*')]
player_txt = askopenfilename(title='选择文件',
filetypes=filetypes,
defaultextension='.txt',
initialdir='./')
with open(player_txt, "r", encoding="utf-8") as f:
self.widget_dic["role"]=f.read()
def select_and_change_image(self, evt):
print("<Button-1>事件未处理", evt)
# filetypes = [("txt files", "*.txt")]
filetypes = [("PNG", "*.png"), ("JPG", "*.jpg")]
player_image = askopenfilename(title='选择图片',
filetypes=filetypes,
defaultextension='.png',
initialdir='C:/Users/Administrator/Desktop')
img = Image.open(player_image)
img = img.resize((270, 530))
self.widget_dic["image"] = ImageTk.PhotoImage(img)
self.widget_dic["tk_label_image_label"].config(image=self.widget_dic["image"])
def computer(self, current_chat):
print("role")
print(self.widget_dic["role"])
return current_chat
def send_button_func(self, evt):
if not self.widget_dic["role"]:
showinfo(title="提示", message="请设置角色")
else:
print("<Button-1>事件未处理", evt)
text_msglist = self.widget_dic["tk_text_chat_win"]
text_msg = self.widget_dic["tk_text_chat_text_input"]
msgcontent = '我:' + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + '\n '
text_msglist.insert(END, msgcontent)
current_input_text = text_msg.get('0.0', END)
text_msglist.insert(END, current_input_text)
# 计算当前多少行
current_input_text_len = len(current_input_text.split("\n"))-1
current_text = text_msglist.get('0.0', END)
current_text_len = len(current_text.split("\n"))
print(current_text_len - 2-current_input_text_len)
# 添加bag
text_msglist.tag_add("name", "{}.0".format(current_text_len - 2 - current_input_text_len),
"{}.26".format(current_text_len - 2 - current_input_text_len))
text_msglist.tag_config("name", font=('微软雅黑', 18, 'bold'), background="gray", foreground="blue",
underline=1)
computertent = '图灵机器人:' + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + '\n '
text_msglist.insert(END, computertent, 'green')
computer_talk = text_msg.get('0.0', END)
current_input_text = self.computer(computer_talk)
text_msglist.insert(END, (current_input_text))
# 计算当前多少行
current_input_text_len = len(current_input_text.split("\n")) - 1
current_text = text_msglist.get('0.0', END)
current_text_len = len(current_text.split("\n"))
print(current_text_len - 2-current_input_text_len)
text_msglist.tag_add("name", "{}.0".format(current_text_len - 2 - current_input_text_len),
"{}.26".format(current_text_len - 2 - current_input_text_len))
text_msglist.tag_config("name", font=('微软雅黑', 8, 'bold'), background="white", foreground="blue",
underline=1)
text_msg.delete('0.0', END)
#保存聊天记录
with open("history.txt", "w", encoding="utf-8") as f:
f.write(text_msglist.get('0.0', END))
def __event_bind(self):
self.widget_dic["tk_label_image_label"].bind('<Button-1>', self.select_and_change_image)
self.widget_dic["tk_button_send_button"].bind('<Button-1>', self.send_button_func)
if __name__ == "__main__":
win = Win()
win.mainloop()
文章来源:https://www.toymoban.com/news/detail-500853.html
到了这里,关于看图角色扮演聊天ui的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!