Hecate__ 2023-12-29 18:12 采纳率: 0%
浏览 22

python人机交互,关于tkinter多页面切换的问题

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()  # 进入消息循环


  • 写回答

2条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2023-12-29 20:29
    关注

    【以下回答由 GPT 生成】

    问题描述

    我正在使用tkinter创建一个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')
    

    请您帮我优化一下问题的描述.



    【相关推荐】



    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    评论

报告相同问题?

问题事件

  • 创建了问题 12月29日