| 
 | 
 
 
发表于 2023-6-27 10:50:47
|
显示全部楼层
   本楼为最佳答案    
 
 
  一个简单的 GUI 界面: 
- import json
 
 - import requests
 
 - from tkinter import *
 
  
- class ChatApp:
 
 -     def __init__(self, master):
 
 -         self.master = master
 
 -         master.title("Chatbot GUI")
 
  
-         # Setting the window size
 
 -         window_width = 500
 
 -         window_height = 500
 
  
-         # Gets both half the screen width/height and window width/height
 
 -         position_right = int(master.winfo_screenwidth()/2 - window_width/2)
 
 -         position_down = int(master.winfo_screenheight()/2 - window_height/2)
 
  
-         # Positions the window in the center of the page.
 
 -         master.geometry(f'{window_width}x{window_height}+{position_right}+{position_down}')
 
  
-         self.userid = 'alsjdasf12'  # replace with your user id
 
 -         self.signature_url = 'https://chatbot.weixin.qq.com/openapi/sign/n1l6sJwy5MLw3SiQTtqDCMqV54XIev'
 
 -         self.chat_url = 'https://chatbot.weixin.qq.com/openapi/aibot/n1l6sJwy5MLw3SiQTtqDCMqV54XIev'
 
 -         self.signature = self.get_signature()
 
  
-         self.chat_frame = Frame(master)
 
 -         self.chat_frame.pack(fill=BOTH, expand=True)
 
  
-         self.scrollbar = Scrollbar(self.chat_frame)
 
 -         self.chat_listbox = Listbox(self.chat_frame, yscrollcommand=self.scrollbar.set)
 
 -         self.scrollbar.config(command=self.chat_listbox.yview)
 
 -         self.chat_listbox.pack(side="left", fill="both", expand=True)
 
 -         self.scrollbar.pack(side="right", fill="y")
 
  
-         self.entry_frame = Frame(master)
 
 -         self.entry_frame.pack(fill=X, side=BOTTOM)
 
  
-         self.entry_text = StringVar()
 
 -         self.entry_field = Entry(self.entry_frame, text=self.entry_text)
 
 -         self.entry_field.bind("<Return>", self.send_message_event)
 
 -         self.entry_field.pack(side="left", fill="x", expand=True)
 
  
-         self.send_button = Button(self.entry_frame, text="发送", command=self.send_message)
 
 -         self.send_button.pack(side="right")
 
  
-         self.new_chat_button = Button(self.entry_frame, text="开始新的对话", command=self.start_new_chat)
 
 -         self.new_chat_button.pack(side="right")
 
  
-     def send_message_event(self, event):
 
 -         self.send_message()
 
  
-     def send_message(self):
 
 -         message = self.entry_text.get()
 
 -         self.chat_listbox.insert(END, "你: " + message)
 
 -         self.entry_text.set("")
 
  
-         response = self.post_chat_message(message)
 
 -         self.chat_listbox.insert(END, "机器人: " + response)
 
  
-     def get_signature(self):
 
 -         data = {
 
 -             "userid": self.userid
 
 -         }
 
 -         response = requests.post(self.signature_url, json=data)
 
 -         response_data = response.json()
 
 -         return response_data['signature']
 
  
-     def post_chat_message(self, query):
 
 -         data = {
 
 -             "signature": self.signature,
 
 -             "query": query
 
 -         }
 
 -         response = requests.post(self.chat_url, json=data)
 
 -         response_data = response.json()
 
 -         return response_data['answer']
 
  
-     def start_new_chat(self):
 
 -         self.chat_listbox.delete(0, END)
 
 -         self.signature = self.get_signature()
 
  
- root = Tk()
 
 - my_gui = ChatApp(root)
 
 - root.mainloop()
 
 
  复制代码 |   
 
评分
- 
查看全部评分
 
 
 
 
 
 |