Reputation: 11
M trying to create a desktop app but facing some problem while switching between frames using button. Its working all fine but it gives me an extra blank window(consist nothing) when I run my project. Below is my code. Please suggest me any changes or error in my code.
import tkinter as tk
class Toplevel1(tk.Tk):
def __init__(self, top=None, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
top.geometry("600x450+306+137")
top.minsize(120, 1)
top.maxsize(1370, 749)
top.resizable(1, 1)
top.title("New Toplevel")
top.configure(background="#d9d9d9")
self.MenuFrame = tk.LabelFrame(top)
self.MenuFrame.place(relx=0.0, rely=0.0, relheight=0.989, relwidth=0.25)
self.MenuFrame.configure(relief='groove')
self.MenuFrame.configure(foreground="black")
self.MenuFrame.configure(background="#400080")
self.Button1 = tk.Button(self.MenuFrame)
self.Button1.place(relx=0.133, rely=0.067, height=24, width=107, bordermode='ignore')
self.Button1.configure(background="#00ff80")
self.Button1.configure(foreground="#000000")
self.Button1.configure(pady="0")
self.Button1.configure(text='''Button 1''')
self.Button1.configure(command= lambda : self.show_frame(ButtonOne))
self.MainWindow = tk.LabelFrame(top)
self.MainWindow.place(relx=0.267, rely=0.111, relheight=0.767, relwidth=0.7)
self.MainWindow.configure(relief='groove')
self.MainWindow.configure(foreground="black")
self.MainWindow.configure(background="#808040")
self.frames = {}
for F in (StartPage, ButtonOne):
frame = F(self.MainWindow)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
# label of frame Layout 2
# second window frame page1
class ButtonOne(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Button 1 is pressed")
label.pack()
if __name__ == '__main__':
root = tk.Tk()
app = Toplevel1(root)
root.mainloop()
Upvotes: 0
Views: 321
Reputation: 385970
This causes a window to be created:
class Toplevel1(tk.Tk):
def __init__(self, top=None, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
...
...
app = Toplevel1(root)
And this causes a window to be created:
root = tk.Tk()
If you don't want Toplevel1
to be a separate window, don't inherit from tk.Tk
. Instead, you can inherit from tk.Frame
, and then you can call pack
, place
, or grid
to add this to the root window.
However, it looks like you're intending for your Toplevel1
to be the true root window, so you can remove root = tk.Tk()
. You can then do app.mainloop()
rather than root.mainloop()
You'll also have to make a few other adjustments, such as using self
rather than top
inside Toplevel1.__init__
.
Put another way, if you want only one window then either inherit from tk.Tk
or create an instance of tk.Tk
, but don't do both.
Upvotes: 1
Reputation: 34
As on tkinter callbacks, tk.Tk in class Toplevel1
is about the same as Toplevel1=tk.Tk()
which, in a sesne opens a new window. the third line from whitespace, tk.Tk.__init__(self, *args, **kwargs)
, it becomes useless.
Upvotes: 0