I am new in python.
I was trying to write a code with tkinter. In click of a button it should open another window and close the previous window. The code is closing the previous window correctly.
But the problem is it is also opening another empty window at the side on the screen.
Here's my code:
# 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()
Edit:
When I remove the " self.first_screen.destroy()" line, then the there is no problem.
Maybe it is because Toplevel needs a parent window. But I need to close the previous window. In this case what should I do?