Toplevel not pack()-ing widgets

Question:

I’m working on a project that I hope will be used for flashcards, but I’ve encountered a problem with TopLevel(). When I run this code, it works exactly how I want it to, except when I click the Review Sets button, it creates a new window but won’t pack any widgets. I’ve used different versions of this code to get it to work like OOP, but it still won’t, always has the same issue. Thoughts?

import requests
from html import unescape
from bs4 import BeautifulSoup
from tkinter import *
import os

def review_button():
    tp = Toplevel()
    tp.title("Flashcard_Studio/Sets")
    tp.minsize(200,200)
    tp.maxsize(200,200)
    label2 = Label(root, text = "Quizlet Link:")
    entry2 = Entry(root, textvariable = entry_text, width = 75)
    print(label2)
    label2.pack()
    entry2.pack()

def scrape(self):
    print("Button pressed")
    HEADERS = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"
    }
    soup = BeautifulSoup(requests.get(entry_text.get(), headers=HEADERS).text, "html.parser")
    flashcard_title = soup.find("h1").text

    f = open(f'flashdata/{flashcard_title}.txt', 'w')

    flsh = soup.find_all("div", class_ = "SetPageTerms-term")
    flsh += soup.find_all("div", class_ = "SetPageTerms-term SetPageTerms-term--beyondSignupThreshold")

    for index, flashcards in enumerate(flsh):
        flshlst = flsh[index]
        flash1 = flshlst.find("a", class_ = "SetPageTerm-wordText").text
        flash2 = flshlst.find("a", class_ = "SetPageTerm-definitionText").text
        with open(f"flashdata/{flashcard_title}.txt", "a") as f:
            f.write(f"w: {flash1}n")
            f.write(f"def: {flash2}n")
            print(f"w: {flash1}n")
            print(f"def: {flash2}n")
    f.close()
    x = open(f"flashdata/{flashcard_title}.txt", "r")
    print(x.read())

root = Tk()

root.title("Flashcard_Studio/Scrape")
root.minsize(700,100)
root.maxsize(700,100)

label = Label(text = "Quizlet Link:")
entry_text = StringVar()
entry = Entry(textvariable = entry_text, width = 75)
button1 = Button(text = "Get Flashcards", command = scrape)
button2 = Button(root, text = "Review Sets", command = review_button)
label.pack()
entry.pack()
button1.pack()
button2.pack()

root.mainloop()

Asked By: Shonky

||

Answers:

If you want the widgets in the toplevel, you must explicitly put them there. Change these lines:

label2 = Label(root, ...)
entry2 = Entry(root, ...)

… to this …

label2 = Label(tp, ...)
entry2 = Entry(tp, ...)
Answered By: Bryan Oakley