1

I'm trying to make a sub window for configuration settings. But the text in the sub window is invisible. I am reading correctly but I cannot see the text. Below is my sample code with the problem:

import tkinter as tk
from tkinter import ttk
from tkinter import Menu


class Frames:
    def __init__(self):
        self.port_com = None

    def main_frame(self, win):

        # Main Frame
        main = ttk.LabelFrame(win, text="")
        main.grid(column=0, row=0, sticky="WENS", padx=10, pady=10)
        return main

    def dut_configuration_frame(self, win):

        # Configuration Frame
        dut_config_frame = ttk.LabelFrame(win, text="Config")
        dut_config_frame.grid(column=0, row=0, sticky='NWS')

        # Port COM
        ttk.Label(dut_config_frame, text="Port COM").grid(column=0, row=0)
        self.port_com = tk.StringVar()
        ttk.Entry(dut_config_frame, width=12, textvariable=self.port_com).grid(column=0, row=1, sticky=tk.EW)
        self.port_com.set(value="COM7")

        print(self.port_com.get())


class ConfigFrames:
    def __init__(self):
        self.port_com = None

    def main_frame(self, win):

        # Main Frame
        main = ttk.LabelFrame(win, text="")
        main.grid(column=0, row=0, sticky="WENS", padx=10, pady=10)
        return main

    def configuration_frame(self, win):

        # Configuration Frame
        dut_config_frame = ttk.LabelFrame(win, text="Config")
        dut_config_frame.grid(column=0, row=0, sticky='NWS')

        # Port COM
        ttk.Label(dut_config_frame, text="Port COM").grid(column=0, row=0)
        self.port_com = tk.StringVar()
        ttk.Entry(dut_config_frame, width=12, textvariable=self.port_com).grid(column=0, row=1, sticky=tk.EW)
        self.port_com.set(value="COM5")

        print(self.port_com.get())


def menu_bar(win):

    def _config():
        config_frame = ConfigFrames()
        config_window = tk.Tk()
        config_window.title("Sub window")
        config_window.geometry("200x200")
        config_window.resizable(0, 0)

        main = config_frame.main_frame(config_window)
        config_frame.configuration_frame(main)
        config_window.mainloop()

    # Menu
    menuBar = Menu(win)
    win.config(menu=menuBar)

    settingsMenu = Menu(menuBar, tearoff=0)
    settingsMenu.add_command(label="Config", command=_config)
    menuBar.add_cascade(label="Settings", menu=settingsMenu)


frames = Frames()

win = tk.Tk()
win.title("Main window")
win.geometry("200x200")
win.resizable(0, 0)
menu_bar(win)
main = frames.main_frame(win)
frames.dut_configuration_frame(win)
win.mainloop()

As you can see in main window it is visible, but in sub window it is invisible. enter image description here

And printing in console is correct: enter image description here

  • 2
    It is because you have used multiple instances of `Tk()`. Use `Toplevel()` (i.e. inside `menu_bar._config()`) for windows other than the main window. – acw1668 May 17 '21 at 07:45
  • @acw1668 Indeed: exchanging `Tk()` by `Toplevel()` in the code fixes the issue. – Wolf May 17 '21 at 07:49
  • It is working! Thanks! – unknow_user May 17 '21 at 07:55
  • @acw1668 wouldn't it be worth to transform your comment into an answer? Maybe it's easy to overlook that `Tk` isn't the same as creating just a top-level window. – Wolf May 17 '21 at 08:09
  • 1
    @Wolf to note: this should be removed if using `Toplevel`: `config_window.mainloop() `, otherwise I would say that this is a quite common mistake so it probably already has answers all over `StackOverflow` for example [here](https://stackoverflow.com/questions/27639298/tkinter-open-a-new-window-with-a-button-prompt) is a similar question in that it asks about opening new windows, but sure adding answer here might help others that somehow wind up on this question and have the same trouble. – Matiiss May 17 '21 at 08:51

0 Answers0