python-3.x - 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 需要一个父窗口。但我需要关闭上一个窗口。在这种情况下我该怎么办?
解决方案
您不能破坏 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")
推荐阅读
- node.js - 在一个名称下导入/导出多个对象和函数
- windows - Smalltalk 相当于 Windows 的检查工具
- c++ - 需要帮助类中的二维数组(C++)
- javascript - Typo3 未捕获的错误:不匹配的匿名定义()模块
- python - discord.py - 按名称在特定频道上发送消息
- python - 在pytest中重用数据结构
- c# - RedirectToAction appends route to URL instead of directing to action in other controller
- angular - Angular:每次我需要更新时,我应该订阅()到 http.get()吗?
- scala - scala - 如何推断返回类型
- ios - 如何在不更改 Bundle Identifier 的情况下将 iOS 应用程序提交到 App Store