简体   繁体   中英

Tkinter labels not showing in pop up window

I started coding with python recently and Stack Overflow seemed to be a source, where all errors I could possible encounter were already asked by someone else and answered. This time I encountered an error and do not find an answer.

I wrote an app, utilizing a GUI with tkinter. In one part of this app the user presses a button from the mainframe to open another window and enter data. After that the user closes the pop up window and the data shall be processed by the program. In this pop up window, labels next to the Entry widgets do not appear.

I googled a lot and tried: - to call the update. method. - checked if the labels appear in the mainframe - re-enabled resizing for the pop up window - isolated the code in a different file and well, here the labels appear

So it seems that something in my main window prevents the labels to appear in the pop up? Or I forgot to tell the program to do something to actively show the labels?

import tkinter as tk, sys
from tkinter import StringVar, Tk
from tkinter.filedialog import askopenfilename

root = tk.Tk()
root.title("title")
w = tk.Label(root, text="text")
w.pack()

# This is the code snippet that works isolated, but not in this context
def enter_deadline():
    det_window = tk.Tk()
    # Enter deadline
    shime_text =  StringVar()
    shime_text.set("〆切月日記入:")
    label_shime=tk.Label(det_window, textvariable=shime_text, height=6)
    label_shime.pack(side="left", padx = 20, pady=20)
    shime_val = StringVar(None)
    det_shime = tk.Entry(det_window,textvariable=shime_val, width=20)
    det_shime.pack(side="left", padx = 20, pady=20)

    def killme():
            det_window.destroy()
    det_button = tk.Button(det_window, text='スタート',command=killme).pack()
    det_window.mainloop()


# Make pop-up window for PO
def create_POW():
    try:
        Tk().withdraw()
        # show an "Open" dialog box and return the path
        po_filename = askopenfilename()
        po_file = open(po_filename, 'rb')
        enter_deadline()
    except Exception as e:
        print("ファイルエラー")
        print(e)
        sys.exit()

    # lots of repititive code for buttons
button3 = tk.Button(root, text='PO',command=create_POW).pack()

root.mainloop()

Since the isolated code snippet does what it should, there seems to be something else I am missing.

Thanks in advance. Andreas

The main problem is that you have created multiple Tk() instances (you create a new one when you click button3 ). So change det_window to instance of Toplevel and remove calling det_window.mainloop() inside enter_deadline() function. Also remove the statement Tk().withdraw() inside create_POW() function.

Below is the modified code with the above changes:

import tkinter as tk, sys
from tkinter import StringVar, Tk
from tkinter.filedialog import askopenfilename

root = tk.Tk()
root.title("title")
w = tk.Label(root, text="text")
w.pack()

# This is the code snippet that works isolated, but not in this context
def enter_deadline():
    det_window = tk.Toplevel()  # changed from tk.Tk()
    # Enter deadline
    shime_text =  StringVar()
    shime_text.set("〆切月日記入:")
    label_shime=tk.Label(det_window, textvariable=shime_text, height=6)
    label_shime.pack(side="left", padx = 20, pady=20)
    shime_val = StringVar(None)
    det_shime = tk.Entry(det_window,textvariable=shime_val, width=20)
    det_shime.pack(side="left", padx = 20, pady=20)

    def killme():
            det_window.destroy()
    tk.Button(det_window, text='スタート',command=killme).pack()
    #det_window.mainloop()


# Make pop-up window for PO
def create_POW():
    try:
        #Tk().withdraw()
        # show an "Open" dialog box and return the path
        po_filename = askopenfilename()
        po_file = open(po_filename, 'rb')
        enter_deadline()
    except Exception as e:
        print("ファイルエラー")
        print(e)
        sys.exit()

    # lots of repititive code for buttons
button3 = tk.Button(root, text='PO',command=create_POW).pack()

root.mainloop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM