tkinter destory问题 读取excel问题

三个问题:
第一个问题,在执行第三组的时候报错

 File "D:\pythonProject1\start.py", line 108, in c
    windows.destroy()
_tkinter.TclError: can't invoke "destroy" command: application has been destroyed

第二个问题,执行第二组的时候第一组界面中点击“出题”按钮没有反应,不会有题显示,是读取excel的问题吗还是?
第三个问题,在运行第二组时会出现名为tk的空白页面小窗口,有没有方法让小窗口不出现


# coding=<encoding name> : # coding=utf-8
import tkinter as tk
from tkinter import Tk
import random
import pandas as pd
from tkinter import *
from pandas.core.frame import DataFrame
from PIL import ImageTk, Image

root = Tk()
root.title("mbti十六型人格测试")
root.geometry("800x800")
root.configure(bg="AntiqueWhite")

image = Image.open(r"C://Users//29212//Desktop//mbti1.jpg")
zoom = 1.8
pixels_x, pixels_y = tuple([int(zoom * x) for x in image.size])
img = ImageTk.PhotoImage(image.resize((600, 400)))
label = Label(root, image=img)
label.image = img
label.pack()

label = tk.Label(
    text="欢迎来到mbti人格测试",
    foreground="white",
    background="purple",
    font="Helvetic 40 bold"
)
label.pack()

label = tk.Label(
    text="请您尽量在十分钟内完成 "
         "并诚实回答"
         "(尽管您并不喜欢这个答案)",
    foreground="white",
    background="DarkTurquoise",
    font="Times 20 "
)
label.pack()

file = r'C:\Users\29212\Desktop\mbtiquestion.xlsx'
#===========================第一组===============
windows = Tk()

def b():
    root.destroy()
    windows.geometry("800x800")
    windows.title("开始答题")
    windows.configure(bg="AntiqueWhite")


btn1 = Button(root, text="开始", width=8, command=b)
btn1.pack()
btn2 = Button(root, text="退出", width=8, command=root.quit)
btn2.pack()

# ---------------出题区-----------------
queslabel = Label(windows, text="请出题")
queslabel.place(x=100, y=200, width=800, height=800)
# ----------------答题区-----------------
Label(windows, text="请输入选择A的个数").pack()
Label(windows, text="请输入选择B的个数").pack()

appeared_indices = set()

def start():
    global file, n, questions, appeared_indices
    data = pd.read_excel(file)

    if len(appeared_indices) == len(data):
        # 所有题目都已经出现过一次,重置集合或列表
        Label(windows, text=("第一组题已全部答完")).pack()

    # 从剩余未出现的题目中随机选择
    remaining_indices = set(range(len(data))) - appeared_indices
    n = random.choice(list(remaining_indices))

    questions = [x for x in data["题组一"].values if not pd.isnull(x)]
    queslabel["text"] = questions[n]

    # 将选择的题目索引添加到已出现集合或列表中
    appeared_indices.add(n)


# ----------------结果显示-----------------
def printinfo():
    a = num1E.get()
    if a >= "4":
        Label(windows, text=("您是外向e型人格")).pack()
    else:
        Label(windows, text=("您是内向i型人格")).pack()


u1 = tk.StringVar()
p1 = tk.StringVar()
num1E = tk.Entry(windows, textvariable=u1)
num1E.pack()
num2 = tk.Entry(windows, textvariable=p1)
num2.pack()
quesButton = Button(windows, text="出题", command=start)
quesButton.place(x=200, y=280)
Button2 = Button(windows, text="输入完毕", command=printinfo)
Button2.place(x=300, y=280)

#======================================第二组====================================
second = Tk()
def c():
     windows.destroy()
     second.geometry("800x800")
     second.title("题组二")
     second.configure(bg="AntiqueWhite")


btn1 = Button(windows, text="下一组", width=8, command=c)
btn1.place(x=500, y=280)

# ---------------出题区-----------------
queslabel = Label(second, text="请出题")
queslabel.place(x=100, y=200, width=800, height=800)
# ----------------答题区-----------------
Label(second, text="请输入选择A的个数").pack()
Label(second, text="请输入选择B的个数").pack()

appeared_indices=set()

def start2():
    global file, n, questions, appeared_indices
    data = pd.read_excel(file)

    if len(appeared_indices) == len(data):
        # 所有题目都已经出现过一次,重置集合或列表
        Label(second, text=("第二组题已全部答完")).pack()

    # 从剩余未出现的题目中随机选择
    remaining_indices = set(range(len(data))) - appeared_indices
    n = random.choice(list(remaining_indices))

    questions = [x for x in data["题组二"].values if not pd.isnull(x)]
    queslabel["text"] = questions[n]

    # 将选择的题目索引添加到已出现集合或列表中
    appeared_indices.add(n)

# ----------------结果显示-----------------
def printinfo2():
    a = num2E.get()
    if a >= "4":
        Label(second, text=("您是抽象N型人格")).pack()
    else:
        Label(second, text=("您是具体S型人格")).pack()


u2 = tk.StringVar()
p2 = tk.StringVar()
num2E = tk.Entry(second, textvariable=u2)
num2E.pack()
num2 = tk.Entry(second, textvariable=p2)
num2.pack()
quesButton = Button(second, text="出题", command=start2)
quesButton.place(x=200, y=280)
Button3= Button(second, text="输入完毕", command=printinfo2)
Button3.place(x=300, y=280)
#==================================第三组======================================

third= Tk()
def d():
     second.destroy()
     third.geometry("800x800")
     third.title("题组二")
     third.configure(bg="AntiqueWhite")


btn2 = Button(second, text="下一组", width=8, command=c)
btn2.place(x=500, y=280)

# ---------------出题区-----------------
queslabel = Label(third, text="请出题")
queslabel.place(x=100, y=200, width=800, height=800)
# ----------------答题区-----------------
Label(third, text="请输入选择A的个数").pack()
Label(third, text="请输入选择B的个数").pack()

appeared_indices=set()

def start3():
    global file, n, questions, appeared_indices
    data = pd.read_excel(file)

    if len(appeared_indices) == len(data):
        # 所有题目都已经出现过一次,重置集合或列表
        Label(third, text=("第三组题已全部答完")).pack()

    # 从剩余未出现的题目中随机选择
    remaining_indices = set(range(len(data))) - appeared_indices
    n = random.choice(list(remaining_indices))

    questions = [x for x in data["题组二"].values if not pd.isnull(x)]
    queslabel["text"] = questions[n]

    # 将选择的题目索引添加到已出现集合或列表中
    appeared_indices.add(n)

# ----------------结果显示-----------------
def printinfo3():
    a = num3E.get()
    if a >= "4":
        Label(third, text=("您是F感觉型人格")).pack()
    else:
        Label(third, text=("您是T思维型人格")).pack()


u3 = tk.StringVar()
p3 = tk.StringVar()
num3E = tk.Entry(third, textvariable=u3)
num3E.pack()
num3 = tk.Entry(third, textvariable=p3)
num3.pack()
quesButton = Button(third, text="出题", command=start3)
quesButton.place(x=200, y=280)
Button3= Button(third, text="输入完毕", command=printinfo3)
Button3.place(x=300, y=280)


third.mainloop()
second.mainloop()
windows.mainloop()

【以下回答由 GPT 生成】

问题:

问题标题: tkinter destory问题 读取excel问题

问题标签: python

三个问题: 1. 在执行第三组时报错:

File "D:\pythonProject1\start.py", line 108, in c
    windows.destroy()
_tkinter.TclError: can't invoke "destroy" command: application has been destroyed
  1. 执行第二组时,第一组界面中点击“出题”按钮没有反应,没有题目显示。是否是读取Excel文件的问题?
  2. 运行第二组时会出现名为tk的空白小窗口,有没有方法让小窗口不出现?

回答:

问题1: windows.destroy()导致_tkinter.TclError报错

当执行windows.destroy()时,出现了_tkinter.TclError: can't invoke "destroy" command: application has been destroyed错误。

这是因为windows窗口在之前的某个地方被销毁(如点击退出按钮),然后你尝试再次销毁它的时候出现的错误。解决这个问题的方法是在调用windows.destroy()之前先检查窗口是否已经被销毁。

你可以使用winfo_exists()方法来检查窗口是否存在,只有在窗口存在的情况下才调用destroy()方法。

if windows.winfo_exists():
    windows.destroy()

问题2: 点击“出题”按钮没有反应,没有显示题目。可能是读取Excel文件的问题。

在代码中没有找到"出题"按钮的具体实现,但可以预测问题可能出现在读取Excel文件。要解决这个问题,你可以按照以下步骤进行检查:

  1. 确保你的Excel文件存在,路径正确,对应代码中的file变量。可以使用绝对路径,并确保文件扩展名(.xlsx)正确。
  2. 使用pandas库的read_excel()方法来读取Excel文件并创建一个数据帧对象。
  3. 确保数据帧对象中包含了你需要的题目数据。
  4. 确保你正确将题目数据显示在GUI界面上,可以使用Label等tkinter部件来显示题目。

以下是可能的代码示例:

# 导入pandas库
import pandas as pd

# 读取Excel文件
df = pd.read_excel(file)

# 确认数据帧对象中的题目数据是否正确
print(df)

# 在GUI界面中显示题目
label = Label(root, text=df['题目'].iloc[0], ...)
label.pack()

问题3: 运行第二组时出现名为tk的空白小窗口,如何避免?

在代码中没有找到第二组的具体实现,所以无法确定问题出现的具体原因。然而,通常情况下,创建一个名为tk的空白小窗口通常是没有意义的。

如果你不希望出现名为tk的小窗口,可以检查你的代码,特别是是否在某处创建了一个名为tk的窗口。请查找你的代码中是否有类似于以下的语句:

tk = Tk()

如果有,你可以将这行代码注释掉或删除,以避免出现名为tk的空白小窗口。请确保整个代码中只创建了一个主窗口,而不是多个。

以上是根据你提供的代码和问题描述,给出的可能解决方案。如果你能提供更多的代码细节和问题背景,可能会有更准确的解决方案。如果你还有其他问题,请随时提问。



【相关推荐】



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