I am trying to create a new window from within a window in Tkinter but all my new content is added to the parent window, not in the new window.
Here is my code:
from tkinter import *
from tkinter import font
import tkinter as tk
NORMAL_PADDING = 3
class AllPages(tk.Frame):
def __init__(self, master):
super().__init__(master)
self.pack()
self.font = font.Font(family='JetBrains Mono', name='jbm', size=12, weight='normal')
self.posts = [
{
"title": "huhu"
}
]
self.posts_container = tk.Frame()
for post in self.posts:
postf = tk.Frame(self.posts_container, bg="#ffffff", borderwidth=1, relief="solid")
post_t = tk.Label(postf, text=post["title"], bg="#ffffff", borderwidth=0, font=self.font)
post_edit = tk.Button(postf, text="Edit", bg="#ffffff", borderwidth=0, relief="solid", highlightbackground="#ffffff", highlightcolor="#ffffff", highlightthickness=1, font=self.font, command= lambda: self.modify_post_form(post))
post_t.pack(side="left")
post_edit.pack(side="right")
postf.pack(anchor=W, fill='x', pady=NORMAL_PADDING)
self.posts_container.pack(fill='both', ipadx=NORMAL_PADDING, padx=NORMAL_PADDING)
def modify_post_form(self, post):
newW = Toplevel()
app = CreatePostForm(newW)
class CreatePostForm(tk.Frame):
def __init__(self, master, post=None):
super().__init__(master)
self.pack()
if not ("jbm" in font.names()):
self.font = font.Font(family='JetBrains Mono', name='jbm', size=12, weight='normal')
else:
self.font = font.nametofont("jbm")
self.label_width = 20
self.input_width = 40
self.title_container = tk.Frame()
self.title_label = tk.Label(self.title_container, text="Title:", font=self.font, width=self.label_width, anchor=W)
self.title_input = tk.Entry(self.title_container, width=self.input_width, borderwidth=1, relief="solid", font=self.font)
self.title_label.pack(side="left")
self.title_input.pack(side="right")
self.title_container.pack(anchor=W, pady=NORMAL_PADDING)
self.title = tk.StringVar()
self.title_input["textvariable"] = self.title
root = tk.Tk()
myapp = AllPages(root)
myapp.pack()
myapp.mainloop()
The first window is created by the AllPages()
class. then AllPages().modify_post_form()
creates a new window using Toplevel()
and pass it to CreatePostForm()
class. CreatePostForm()
should then populates the new window with some new content but whatever CreatePostForm()
creates is going to the parent window, not to the new window created using Toplevel()
.
How can I fix this?
Thank you.
CodePudding user response:
The problem in the above code is that both class
es didn't specify the window it should create content into. Here is the working code:
from tkinter import *
from tkinter import font
import tkinter as tk
NORMAL_PADDING = 3
class AllPages(tk.Frame):
def __init__(self, master):
super().__init__(master)
self.pack()
self.font = font.Font(family='JetBrains Mono', name='jbm', size=12, weight='normal')
self.posts = [
{
"title": "huhu"
}
]
self.posts_container = tk.Frame(self)
for post in self.posts:
postf = tk.Frame(self.posts_container, bg="#ffffff", borderwidth=1, relief="solid")
post_t = tk.Label(postf, text=post["title"], bg="#ffffff", borderwidth=0, font=self.font)
post_edit = tk.Button(postf, text="Edit", bg="#ffffff", borderwidth=0, relief="solid", highlightbackground="#ffffff", highlightcolor="#ffffff", highlightthickness=1, font=self.font, command= lambda: self.modify_post_form(post))
post_t.pack(side="left")
post_edit.pack(side="right")
postf.pack(anchor=W, fill='x', pady=NORMAL_PADDING)
self.posts_container.pack(fill='both', ipadx=NORMAL_PADDING, padx=NORMAL_PADDING)
def modify_post_form(self, post):
newW = Toplevel()
app = CreatePostForm(newW)
class CreatePostForm(tk.Frame):
def __init__(self, master, post=None):
super().__init__(master)
self.pack()
if not ("jbm" in font.names()):
self.font = font.Font(family='JetBrains Mono', name='jbm', size=12, weight='normal')
else:
self.font = font.nametofont("jbm")
self.label_width = 20
self.input_width = 40
self.title_container = tk.Frame(self)
self.title_label = tk.Label(self.title_container, text="Title:", font=self.font, width=self.label_width, anchor=W)
self.title_input = tk.Entry(self.title_container, width=self.input_width, borderwidth=1, relief="solid", font=self.font)
self.title_label.pack(side="left")
self.title_input.pack(side="right")
self.title_container.pack(anchor=W, pady=NORMAL_PADDING)
self.title = tk.StringVar()
self.title_input["textvariable"] = self.title
root = tk.Tk()
myapp = AllPages(root)
myapp.pack()
myapp.mainloop()
Thank you to all the people who commented and helped me.