一共做了五个窗口,想着点入下一个窗口后前一个窗口自动销毁,可为什么我开始运行后所有窗口一块弹出来
# 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")
# image1 = Image.open(r"C://Users//29212//Desktop//mbti1.jpg")
# zoom = 1.8
# pixels_x1, pixels_y1 = tuple([int(zoom * x) for x in image1.size])
# img1 = ImageTk.PhotoImage(image1.resize((600, 400)))
# label1 = Label(root, image=img1)
# label1.image = img1
# label1.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()
# ---------------出题区-----------------
queslabel1 = Label(windows, text="请出题")
queslabel1.place(x=100, y=150, width=900, height=800)
# ----------------答题区-----------------
Label(windows, text="请输入选择A的个数").pack()
Label(windows, text="请输入选择B的个数").pack()
appeared_indices= set()
def start():
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))
questions1 = [x for x in data["题组一"].values if not pd.isnull(x)]
queslabel1["text"] = questions1[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)
Button9 = Button(windows, text="输入完毕", command=printinfo)
Button9.place(x=400, y=280)
# ======================================第二组====================================
second = Tk()
def c():
if windows.winfo_exists():
windows.destroy()
second.geometry("800x800")
second.title("题组二")
second.configure(bg="AntiqueWhite")
btn1 = Button(windows, text="下一组", width=8, command=c)
btn1.place(x=600, y=280)
# ---------------出题区-----------------
queslabel2 = Label(second, text="请出题")
queslabel2.place(x=100, y=200, width=800, height=800)
# ----------------答题区-----------------
Label(second, text="请输入选择A的个数").pack()
Label(second, text="请输入选择B的个数").pack()
appeared_indices1 = set()
def start2():
data = pd.read_excel(file)
if len(appeared_indices1) == len(data):
# 所有题目都已经出现过一次,重置集合或列表
Label(second, text=("第二组题已全部答完")).pack()
# 从剩余未出现的题目中随机选择
remaining_indices = set(range(len(data))) - appeared_indices1
n = random.choice(list(remaining_indices))
questions2= [x for x in data["题组二"].values if not pd.isnull(x)]
queslabel2["text"] = questions2[n]
# 将选择的题目索引添加到已出现集合或列表中
appeared_indices1.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()
num3 = tk.Entry(second, textvariable=p2)
num3.pack()
quesButton = Button(second, text="出题", command=start2)
quesButton.place(x=200, y=280)
Button3 = Button(second, text="输入完毕", command=printinfo2)
Button3.place(x=400, y=280)
# ==================================第三组======================================
third = Tk()
def d():
second.destroy()
third.geometry("800x800")
third.title("题组三")
third.configure(bg="AntiqueWhite")
btn4 = Button(second, text="下一组", width=8, command=d)
btn4.place(x=600, y=280)
# ---------------出题区-----------------
queslabel3 = Label(third, text="请出题")
queslabel3.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))
questions3 = [x for x in data["题组三"].values if not pd.isnull(x)]
queslabel3["text"] = questions3[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=400, y=280)
#===================================第四组========================
fourth = Tk()
def e():
# fourth.Toplevel()
third.destroy()
fourth.geometry("800x800")
fourth.title("题组四")
fourth.configure(bg="AntiqueWhite")
btn5 = Button(third, text="下一组", width=8, command=e)
btn5.place(x=600, y=280)
# ---------------出题区-----------------
queslabel4 = Label(fourth, text="请出题")
queslabel4.place(x=100, y=200, width=800, height=800)
# ----------------答题区-----------------
Label(fourth, text="请输入选择A的个数").pack()
Label(fourth, text="请输入选择B的个数").pack()
appeared_indices = set()
def start4():
data = pd.read_excel(file)
if len(appeared_indices) == len(data):
# 所有题目都已经出现过一次,重置集合或列表
Label(fourth, text=("第四组题已全部答完")).pack()
# 从剩余未出现的题目中随机选择
remaining_indices = set(range(len(data))) - appeared_indices
n = random.choice(list(remaining_indices))
questions4 = [x for x in data["题组四"].values if not pd.isnull(x)]
queslabel4["text"] = questions4[n]
# 将选择的题目索引添加到已出现集合或列表中
appeared_indices.add(n)
# ----------------结果显示-----------------
def printinfo4():
a = num4E.get()
if a >= "4":
Label(fourth, text=("您是J判断型人格")).pack()
else:
Label(fourth, text=("您是P感受型人格")).pack()
u4 = tk.StringVar()
p4 = tk.StringVar()
num4E = tk.Entry(fourth, textvariable=u4)
num4E.pack()
num3 = tk.Entry(fourth, textvariable=p4)
num3.pack()
quesButton = Button(fourth, text="出题", command=start4)
quesButton.place(x=200, y=280)
Button3 = Button(fourth, text="输入完毕", command=printinfo4)
Button3.place(x=400, y=280)
#===================================最终结果显示=====================================
fifth= Tk()
def f():
fifth = Toplevel()
fourth.destroy()
fifth.geometry("800x800")
fifth.title("最终结果")
fifth.configure(bg="AntiqueWhite")
image2 = Image.open(r"C:\Users\29212\Desktop\results.jpg")
zoom = 1.8
pixels_x2, pixels_y2 = tuple([int(zoom * x) for x in image2.size])
img2 = ImageTk.PhotoImage(image2.resize((600, 400)))
label2 = Label(fifth, image=img2)
label2.image = img2
label2.pack()
image3 = Image.open(r"C:\Users\29212\Desktop\results2.jpg")
zoom = 1.8
pixels_x3, pixels_y3 = tuple([int(zoom * x) for x in image3.size])
img3 = ImageTk.PhotoImage(image3.resize((600, 400)))
label3 = Label(fifth, image=img3)
label3.image = img3
label3.pack()
btn6 = Button(fourth, text="最终结果显示", width=8, command=f)
btn6.place(x=600, y=280)
image1 = Image.open(r"C://Users//29212//Desktop//mbti1.jpg")
zoom = 1.8
pixels_x1, pixels_y1 = tuple([int(zoom * x) for x in image1.size])
img1 = ImageTk.PhotoImage(image1.resize((600, 400)))
label1 = Label(root, image=img1)
label1.image = img1
label1.pack()
root.mainloop()
做的挺有意思,在你的代码上做了个模板,用了ithdraw来隐藏窗口,需要显示窗口时再使用deiconify()方法来显示窗口。只要在修改一点点就是你最后的结果
import tkinter as tk
from tkinter import filedialog
from tkinter import messagebox
import pandas as pd
import random
root = tk.Tk()
root.title("人格测试")
root.geometry("600x400")
file = ""
appeared_indices = set()
def openfile():
global file
file = filedialog.askopenfilename(filetypes=[("Excel Files", "*.xlsx")])
filelabel.config(text=file)
def start1():
data = pd.read_excel(file)
if len(appeared_indices) == len(data):
# 所有题目都已经出现过一次,重置集合或列表
messagebox.showinfo("提示", "第一组题已全部答完")
appeared_indices.clear()
return
# 从剩余未出现的题目中随机选择
remaining_indices = set(range(len(data))) - appeared_indices
n = random.choice(list(remaining_indices))
questions1 = [x for x in data["题组一"].values if not pd.isnull(x)]
queslabel1.config(text=questions1[n])
# 将选择的题目索引添加到已出现集合或列表中
appeared_indices.add(n)
def printinfo1():
a = num1E.get()
if a >= "4":
messagebox.showinfo("结果", "您是E外向型人格")
else:
messagebox.showinfo("结果", "您是I内向型人格")
def start2():
data = pd.read_excel(file)
if len(appeared_indices) == len(data):
# 所有题目都已经出现过一次,重置集合或列表
messagebox.showinfo("提示", "第二组题已全部答完")
appeared_indices.clear()
return
# 从剩余未出现的题目中随机选择
remaining_indices = set(range(len(data))) - appeared_indices
n = random.choice(list(remaining_indices))
questions2 = [x for x in data["题组二"].values if not pd.isnull(x)]
queslabel2.config(text=questions2[n])
# 将选择的题目索引添加到已出现集合或列表中
appeared_indices.add(n)
def printinfo2():
a = num2E.get()
if a >= "4":
messagebox.showinfo("结果", "您是S感觉型人格")
else:
messagebox.showinfo("结果", "您是N直觉型人格")
def start3():
data = pd.read_excel(file)
if len(appeared_indices) == len(data):
# 所有题目都已经出现过一次,重置集合或列表
messagebox.showinfo("提示", "第三组题已全部答完")
appeared_indices.clear()
return
# 从剩余未出现的题目中随机选择
remaining_indices = set(range(len(data))) - appeared_indices
n = random.choice(list(remaining_indices))
questions3 = [x for x in data["题组三"].values if not pd.isnull(x)]
queslabel3.config(text=questions3[n])
# 将选择的题目索引添加到已出现集合或列表中
appeared_indices.add(n)
def printinfo3():
a = num3E.get()
if a >= "4":
messagebox.showinfo("结果", "您是T思考型人格")
else:
messagebox.showinfo("结果", "您是F情感型人格")
def show_second():
second.deiconify()
root.withdraw()
def show_third():
third.deiconify()
second.withdraw()
def show_root():
root.deiconify()
third.withdraw()
filebutton = tk.Button(root, text="选择文件", command=openfile)
filebutton.place(x=200, y=10)
filelabel = tk.Label(root, text="未选择文件")
filelabel.place(x=300, y=10)
queslabel1 = tk.Label(root, text="")
queslabel1.pack()
num1E = tk.StringVar()
num1 = tk.Entry(root, textvariable=num1E)
num1.pack()
quesButton1 = tk.Button(root, text="出题", command=start1)
quesButton1.place(x=200, y=280)
Button1 = tk.Button(root, text="输入完毕", command=printinfo1)
Button1.place(x=400, y=280)
second = tk.Toplevel(root)
second.withdraw()
queslabel2 = tk.Label(second, text="")
queslabel2.pack()
num2E = tk.StringVar()
num2 = tk.Entry(second, textvariable=num2E)
num2.pack()
quesButton2 = tk.Button(second, text="出题", command=start2)
quesButton2.place(x=200, y=280)
Button2 = tk.Button(second, text="输入完毕", command=printinfo2)
Button2.place(x=400, y=280)
third = tk.Toplevel(root)
third.withdraw()
queslabel3 = tk.Label(third, text="")
queslabel3.pack()
num3E = tk.StringVar()
num3 = tk.Entry(third, textvariable=num3E)
num3.pack()
quesButton3 = tk.Button(third, text="出题", command=start3)
quesButton3.place(x=200, y=280)
Button3 = tk.Button(third, text="输入完毕", command=printinfo3)
Button3.place(x=400, y=280)
nextButton1 = tk.Button(root, text="下一组题", command=show_second)
nextButton1.place(x=200, y=320)
nextButton2 = tk.Button(second, text="下一组题", command=show_third)
nextButton2.place(x=200, y=320)
prevButton2 = tk.Button(second, text="上一组题", command=show_root)
prevButton2.place(x=300, y=320)
prevButton3 = tk.Button(third, text="上一组题", command=show_second)
prevButton3.place(x=300, y=320)
root.mainloop()
题主,这个问题我来替你解决,若有帮助,还望采纳,点击回答右侧采纳即可。
问题在于你创建了多个Tkinter的根窗口(Tk()),但是一个应用程序只需要一个根窗口。如果要创建多个窗口,应该使用Toplevel()函数创建子窗口。
所以,如果想实现“点入下一个窗口后前一个窗口自动销毁”,应该使用Toplevel()函数来创建子窗口,并且在子窗口中使用destroy()函数销毁父窗口。同时,需要注意在子窗口中使用mainloop()会导致主程序的阻塞,应该避免这种情况的发生。
另外,建议将所有的窗口和控件放在类中,并使用面向对象的方式来编写代码,这样会更加清晰和易于维护。
按照这种方式试试
from tkinter import *
# 创建主窗口
root = Tk()
root.title("Window 1")
# 创建第二个窗口,但先隐藏起来
window2 = Toplevel(root)
window2.title("Window 2")
window2.withdraw()
# 创建第三个窗口,但先隐藏起来
window3 = Toplevel(root)
window3.title("Window 3")
window3.withdraw()
# 定义一个函数来销毁当前窗口并显示下一个窗口
def next_window(current, next):
current.withdraw() # 先隐藏当前窗口
next.deiconify() # 再显示下一个窗口
# 在每个窗口上添加一个按钮来调用这个函数
Button(root, text="Next", command=lambda: next_window(root, window2)).pack()
Button(window2, text="Next", command=lambda: next_window(window2, window3)).pack()
Button(window3, text="Next", command=lambda: next_window(window3, root)).pack()
# 开始主循环
root.mainloop()
【以下回答由 GPT 生成】
我希望解决tkinter运行后所有窗口一起弹出的问题,并优化代码。
解决方案如下: 1. 问题分析:当前的代码中,你使用了多个Tk()实例来创建窗口,这就导致了所有窗口一起弹出的问题。
代码修改如下:
# coding=utf-8
import tkinter as tk
from tkinter import Tk, Toplevel
from tkinter import *
import random
import pandas as pd
from pandas.core.frame import DataFrame
from PIL import ImageTk, Image
root = Tk()
root.title("mbti十六型人格测试")
root.geometry("800x800")
root.configure(bg="AntiqueWhite")
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 = Toplevel(root)
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()
# ---------------出题区-----------------
queslabel1 = Label(windows,
请注意,其他窗口的创建方式应该修改为类似的方式,即使用Toplevel()
而不是Tk()
来创建。
希望这个解决方案对你有帮助!如果你还有其他问题,请随时提问。
【相关推荐】
该回答通过自己思路及引用到GPTᴼᴾᴱᴺᴬᴵ搜索,得到内容具体如下:
根据你上面的代码,看到你使用了多个Tk()实例来创建窗口。在Tkinter中,应该只创建一个Tk()实例作为主窗口,而使用Toplevel()实例来创建其他子窗口。
以下是修改后的代码,确保每次点击"下一组"按钮时,上一个窗口会自动销毁:
from tkinter import Tk, Toplevel, Button, Label, Entry, StringVar
from PIL import Image, ImageTk
import pandas as pd
import random
# 创建主窗口
root = Tk()
root.geometry("800x800")
root.title("MBTI测试")
root.configure(bg="AntiqueWhite")
# 加载题库文件
file = "题库.xlsx"
# 定义题目索引集合
appeared_indices = set()
def start_quiz(question_set, question_label):
data = pd.read_excel(file)
if len(appeared_indices) == len(data):
# 所有题目都已经出现过一次,重置集合
Label(root, text=("所有题目已答完")).pack()
return
# 从剩余未出现的题目中随机选择
remaining_indices = set(range(len(data))) - appeared_indices
n = random.choice(list(remaining_indices))
questions = [x for x in data[question_set].values if not pd.isnull(x)]
question_label["text"] = questions[n]
# 将选择的题目索引添加到已出现集合中
appeared_indices.add(n)
def print_result(personality_type):
result_window = Toplevel(root)
result_window.geometry("800x800")
result_window.title("最终结果")
result_window.configure(bg="AntiqueWhite")
image = Image.open("results.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(result_window, image=img)
label.image = img
label.pack()
image2 = Image.open("results2.jpg")
zoom = 1.8
pixels_x2, pixels_y2 = tuple([int(zoom * x) for x in image2.size])
img2 = ImageTk.PhotoImage(image2.resize((600, 400)))
label2 = Label(result_window, image=img2)
label2.image = img2
label2.pack()
Label(result_window, text=f"您的人格类型是:{personality_type}").pack()
def start_second():
if second.winfo_exists():
second.destroy()
second = Toplevel(root)
second.geometry("800x800")
second.title("题组二")
second.configure(bg="AntiqueWhite")
Button(second, text="下一组", width=8, command=start_third).pack()
ques_label = Label(second, text="请出题")
ques_label.pack()
start_quiz("题组二", ques_label)
Label(second, text="请输入选择A的个数").pack()
Label(second, text="请输入选择B的个数").pack()
num_A = StringVar()
num_B = StringVar()
num_A_entry = Entry(second, textvariable=num_A)
num_A_entry.pack()
num_B_entry = Entry(second, textvariable=num_B)
num_B_entry.pack()
Button(second, text="出题", command=lambda: start_quiz("题组二", ques_label)).pack()
Button(second, text="输入完毕", command=lambda: print_result("抽象N型人格" if num_A.get() >= "4" else "具体S型人格")).pack()
def start_third():
if third.winfo_exists():
third.destroy()
third = Toplevel(root)
third.geometry("800x800")
third.title("题组三")
third.configure(bg="AntiqueWhite")
Button(third, text="下一组", width=8, command=start_fourth).pack()
ques_label = Label(third, text="请出题")
ques_label.pack()
start_quiz("题组三", ques_label)
Label(third, text="请输入选择A的个数").pack()
Label(third, text="请输入选择B的个数").pack()
num_A = StringVar()
num_B = StringVar()
num_A_entry = Entry(third, textvariable=num_A)
num_A_entry.pack()
num_B_entry = Entry(third.pack()
Button(third, text="出题", command=lambda: start_quiz("题组三", ques_label)).pack()
Button(third, text="输入完毕", command=lambda: print_result("思考T型人格" if num_A.get() >= "4" else "情感F型人格")).pack()
def start_fourth():
if fourth.winfo_exists():
fourth.destroy()
fourth = Toplevel(root)
fourth.geometry("800x800")
fourth.title("题组四")
fourth.configure(bg="AntiqueWhite")
Button(fourth, text="下一组", width=8, command=start_fifth).pack()
ques_label = Label(fourth, text="请出题")
ques_label.pack()
start_quiz("题组四", ques_label)
Label(fourth, text="请输入选择A的个数").pack()
Label(fourth, text="请输入选择B的个数").pack()
num_A = StringVar()
num_B = StringVar()
num_A_entry = Entry(fourth, textvariable=num_A)
num_A_entry.pack()
num_B_entry = Entry(fourth, textvariable=num_B)
num_B_entry.pack()
Button(fourth, text="出题", command=lambda: start_quiz("题组四", ques_label)).pack()
Button(fourth, text="输入完毕", command=lambda: print_result("判断J型人格" if num_A.get() >= "4" else "感知P型人格")).pack()
def start_fifth():
if fifth.winfo_exists():
fifth.destroy()
fifth = Toplevel(root)
fifth.geometry("800x800")
fifth.title("最终结果")
fifth.configure(bg="AntiqueWhite")
image = Image.open("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(fifth, image=img)
label.image = img
label.pack()
Button(root, text="开始测试", command=start_second).pack()
root.mainloop()
这段代码修复了窗口重叠的问题,并且只在每个组的开始创建新窗口,并在跳转到下一组时销毁前一个窗口。在主窗口中点击"开始测试"按钮将开始测试过程,依次显示题组二、题组三、题组四和最终结果。
如果以上回答对您有所帮助,点击一下采纳该答案~谢谢