首页 > 解决方案 > tkinter 中的顶层正在旁边创建另一个空窗口

问题描述

我是python新手。

我试图用 tkinter 编写代码。单击按钮时,它应该打开另一个窗口并关闭前一个窗口。代码正确关闭了前一个窗口。

但问题是它也在屏幕侧面打开了另一个空窗口。

这是我的代码:

# The first part got no problem

from tkinter import *
import time


class Start:
    def __init__(self):

        self.first_screen = Tk()

        self.win_width = 500
        self.win_height = 500

        self.screen_width = self.first_screen.winfo_screenwidth()
        self.screen_height = self.first_screen.winfo_screenheight()

        self.x_position = (self.screen_width / 2) - (self.win_width / 2)
        self.y_position = (self.screen_height / 2) - (self.win_height / 2)

        self.first_screen.title("Number game")
        self.first_screen.config(bg="#ffff00")
        self.first_screen.geometry("%dx%d+%d+%d" % (self.win_width, self.win_height, self.x_position, self.y_position))

        self.btn_play = Button(self.first_screen, text="Start", command=self.btn_play_click_action, width="10")
        self.btn_play.pack(side="top")
        self.btn_play.place(height=40, width=200, x=150, y=200)

        self.first_screen.mainloop()

        # This is where the problem happened
    def btn_play_click_action(self):
        time.sleep(1)
        self.first_screen.destroy()
        self.second_screen = Toplevel()
        self.second_screen.geometry("%dx%d+%d+%d" % (self.win_width, self.win_height, self.x_position, self.y_position))
        self.second_screen.title("Number game")
        self.second_screen.config(bg="#eeee00")
        self.label1 = Label(self.second_screen, width=50, bg="#000000")

        self.label1.pack(side="top")
        self.second_screen.mainloop()



Start()

编辑:

当我删除“self.first_screen.destroy()”行时,就没有问题了。

也许是因为 Toplevel 需要一个父窗口。但我需要关闭上一个窗口。在这种情况下我该怎么办?

标签: python-3.xtkintertoplevel

解决方案


您不能破坏 self.first_screen = Tk() 程序的根窗口。此外,您不需要顶级窗口的主循环。您可以使用 .withdraw() 方法来隐藏根窗口而不是 .destroy()

这是您更新的代码-

from tkinter import *
import time


class Start:
    def __init__(self):

        self.first_screen = Tk()

        self.win_width = 500
        self.win_height = 500

        self.screen_width = self.first_screen.winfo_screenwidth()
        self.screen_height = self.first_screen.winfo_screenheight()

        self.x_position = (self.screen_width / 2) - (self.win_width / 2)
        self.y_position = (self.screen_height / 2) - (self.win_height / 2)

        self.first_screen.title("Number game")
        self.first_screen.config(bg="#ffff00")
        self.first_screen.geometry("%dx%d+%d+%d" % (self.win_width, self.win_height, self.x_position, self.y_position))

        self.btn_play = Button(self.first_screen, text="Start", command=self.btn_play_click_action, width="10")
        self.btn_play.pack(side="top")
        self.btn_play.place(height=40, width=200, x=150, y=200)

        self.first_screen.mainloop()

        # This is where the problem happened
    def btn_play_click_action(self):
        time.sleep(1)
        self.first_screen.withdraw()
        self.second_screen = Toplevel()
        self.second_screen.geometry("%dx%d+%d+%d" % (self.win_width, self.win_height, self.x_position, self.y_position))
        self.second_screen.title("Number game")
        self.second_screen.config(bg="#eeee00")
        self.label1 = Label(self.second_screen, width=50, bg="#000000")

        self.label1.pack(side="top")

推荐阅读