0

I am trying to display a popup message in tkinter but I don't know why it's not showing in the window. Here is my code :

def popupmsg(msg):
    popup = tk.Tk()
    popup.wm_title("Results ")

    data_string = tk.StringVar()
    data_string.set(msg)
    ent = tk.Entry(popup, textvariable=data_string, fg="black", bg="white", bd=0, state="readonly")
    ent.pack(side="top", fill="x", pady=10)
    B1 = tk.Button(popup, text="Okay", command=popup.destroy)
    B1.pack()
    popup.mainloop()

This is how I am calling :

string = " "
for f in final:
    string = string + " " + f + " \n"
# print("Results : \n" + string)
popupmsg("Results : \n" + string)

This is I what I get : Msg is missing enter image description here

Hamza Arshad
  • 35
  • 1
  • 7

2 Answers2

0

Try this...

import tkinter as tk
def popupmsg(msg):
    popup = tk.Tk()
    popup.wm_title("Results ")
    data_string = tk.StringVar()
    data_string.set(msg)
    ent = tk.Entry(popup, textvariable=data_string, fg="black", bg="white", bd=0)
    ent.configure(state='readonly')
    ent.pack(side="top", fill="x", pady=10)
    B1 = tk.Button(popup, text="Okay", command=popup.destroy)
    B1.pack()
    popup.mainloop()
Gnanavel
  • 740
  • 6
  • 18
0

You are using more than one instance of Tk(). So the StringVar() doesnt know which parent to belong to, so to fix this, try replacing all child Tk() with Toplevel() leaving you with just one Tk().

The non recommended way would be to say data_string = tk.StringVar(master=popup) but still creating two instance of Tk() is gonna cause another bunch of problems on the way. Why are multiple instances of Tk discouraged?

Since your not getting the output only and no error is shown, this would be the obvious error, if this does not work, please provide more debugging details or some more code.

Delrius Euphoria
  • 14,910
  • 3
  • 15
  • 46