py课程最后要求做的gui界面
舍友给了我他写的问答界面 我负责将三个界面整合在一起
用的是多页面切换
但是被我修改的界面一点开后,界面23不会覆盖显示
或者在点击第一个界面后再点击第二个界面,第二个界面完全就没有显示了
(原来是在第一个界面的下一个图层,将窗口拉大可以看见)
想问问怎么修改
真的不会做
下面是代码:
import tkinter as tk
import json
import random
import requests
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
# 聊天
client_id = '9IRpd0Wo2dSv6aIqxxNMKnM9'
client_secret = 'Tt3gLH8KKQXiPIynVBZpDWQ4GymIKsim'
# 聊天
def unit_chat(chat_input, user_id=8888):
chat_reply = '谢谢,对方不在,请稍等'
url = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=%s&client_secret=%s" % (
client_id, client_secret)
ret = requests.get(url)
access_token = eval(ret.text)['access_token']
post_data = {
"log_id": str(random.random()),
"request": {
"query": chat_input,
"user_id": user_id
},
"session_id": "",
"service_id": "S103921",
"version": "3.0"
}
unit_chatbot_url = "https://aip.baidubce.com/rpc/2.0/unit/service/chat?access_token=" + access_token
res = requests.post(unit_chatbot_url, json=post_data)
unit_chat_obj = json.loads(res.content)
if unit_chat_obj['error_code'] != 0:
return chat_reply
unit_chat_result = unit_chat_obj['result']
unit_chat_response_list = unit_chat_result['response_list']
unit_chat_response_obj = random.choice([unit_chat_response for unit_chat_response in unit_chat_response_list if
unit_chat_response['schema']['intent_confidence'] > 0.0])
unit_chat_action = unit_chat_response_obj['action_list']
unit_chat_action_obj = random.choice(unit_chat_action)
unit_chat_say = unit_chat_action_obj['say']
return unit_chat_say
class ChatWindow:
def __init__(self, master):
self.master = master
self.master.title('Chatbot')
self.master.geometry('500x600')
self.style = ttk.Style()
self.style.configure('TFrame', background='#D3d3d3')
self.style.configure('TButton', background='#D3d3d3', foreground='black', font=('Arial', 12))
self.style.configure('TEntry', font=('Arial', 12))
self.chat_frame = ttk.Frame(self.master)
self.chat_frame.pack(side=TOP, fill=BOTH, expand=True)
self.chat_history = Text(self.chat_frame, width=70, height=22, wrap=WORD, font=('Arial', 12))
self.chat_history.pack(side=TOP, fill=BOTH, padx=10, pady=10)
self.user_input = ttk.Entry(self.master, width=70, font=('Arial', 12))
self.user_input.pack(side=TOP, pady=(10, 0), padx=10)
self.send_button = ttk.Button(self.master, text='Send', command=self.send_message)
self.send_button.pack(side=TOP, pady=5)
self.user_input.bind('<Return>', self.handle_send)
self.chat_history.tag_configure("unit", foreground='black')
self.chat_history.tag_configure("user", foreground='#4e72b8')
def handle_send(self, event):
self.send_message()
def send_message(self):
chat_input = self.user_input.get()
if chat_input.lower() == 'bye':
self.master.quit()
else:
result_say = unit_chat(chat_input)
self.display_message(f'You: {chat_input}\n', 'user')
self.display_message(f'Unit: {result_say}\n', 'unit')
self.user_input.delete(0, END)
def display_message(self, message, sender):
self.chat_history.insert(END, message, sender)
self.chat_history.see(END)
def unit_chat(chat_input, user_id=8888):
chat_reply = '谢谢,对方不在,请稍等'
url = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=%s&client_secret=%s" % (
client_id, client_secret)
ret = requests.get(url)
access_token = eval(ret.text)['access_token']
post_data = {
"log_id": str(random.random()),
"request": {
"query": chat_input,
"user_id": user_id
},
"session_id": "",
"service_id": "S103921",
"version": "3.0"
}
unit_chatbot_url = "https://aip.baidubce.com/rpc/2.0/unit/service/chat?access_token=" + access_token
res = requests.post(unit_chatbot_url, json=post_data)
unit_chat_obj = json.loads(res.content)
if unit_chat_obj['error_code'] != 0:
return chat_reply
unit_chat_result = unit_chat_obj['result']
unit_chat_response_list = unit_chat_result['response_list']
unit_chat_response_obj = random.choice([unit_chat_response for unit_chat_response in unit_chat_response_list if
unit_chat_response['schema']['intent_confidence'] > 0.0])
unit_chat_action = unit_chat_response_obj['action_list']
unit_chat_action_obj = random.choice(unit_chat_action)
unit_chat_say = unit_chat_action_obj['say']
return unit_chat_say
# 按钮对应的功能
def create_frame1():
global frame3, frame2, frame1
try:
frame1.destroy()
except:
pass
finally:
try:
frame2.destroy()
except:
pass
finally:
try:
frame3.destroy()
except:
pass
finally:
frame1 = tk.Frame(window)
frame1 = ChatWindow(window)
def create_frame2():
global frame3, frame2, frame1
try:
frame1.destroy()
except:
pass
finally:
try:
frame3.destroy()
except:
pass
finally:
frame2 = tk.Frame(window, height=500, width=500, bg='blue')
frame2.pack(side='bottom', fill='both', expand=1)
frame2.pack_propagate(0)
label = tk.Label(frame2,
text='界面二',
font=('微软雅黑', 18),
bg='blue')
label.pack()
def create_frame3():
global frame3, frame2, frame1
try:
frame1.destroy()
except:
pass
finally:
try:
frame2.destroy()
except:
pass
finally:
frame3 = tk.Frame(window, height=500, width=500, bg='green')
frame3.pack(side='bottom', fill='both', expand=1)
frame3.pack_propagate(0)
label = tk.Label(frame3,
text='界面三',
font=('微软雅黑', 18),
bg='green')
label.pack()
# 创建窗口
window = tk.Tk()
window.geometry('500x600')
window.title("多窗口切换")
# 按钮框架
frame0 = tk.Frame(window, height=100, width=500)
frame0.pack(side='top', fill='both', expand=1)
# 界面切换按钮
btn1 = tk.Button(frame0, text='界面一', command=create_frame1)
btn1.place(relx=0, rely=0, relwidth=0.3, relheight=1)
btn2 = tk.Button(frame0, text='界面二', command=create_frame2)
btn2.place(relx=0.3, rely=0, relwidth=0.3, relheight=1)
btn3 = tk.Button(frame0, text='界面三', command=create_frame3)
btn3.place(relx=0.6, rely=0, relwidth=0.4, relheight=1)
# 首先打开主界面
create_frame1()
window.mainloop() # 进入消息循环