-1

I am trying to make a UI of a game and I am using a tkinter for creating this UI. My problem is that how can I create a pop-up display inside a class. This is how code works.

if __name__ == '__main__':
root = tk.Tk()
root.geometry("480x320")

root['bg']='white'
PlayerTwo=PhotoImage(file="2.png")
PlayerThree=PhotoImage(file="3.png")
PlayerFour=PhotoImage(file="4.png")

players_label = tk.Label(root,
                         text="CHOOSE THE NUMBER OF PLAYERS THAT WILL BE PLAYING",
                         font="Times 13",
                         fg="white",
                         bg="#85C1E9")
players_label.pack(fill=X,ipady=40)


#No. of players and button for players name
b0 = tk.Button(root, text="Click here after player credentials", command=determine_players)
b1 = tk.Button(root, image=PlayerTwo, command=lambda : pop_up1(root))
b2 = tk.Button(root, image=PlayerThree, command=lambda : pop_up2(root))
b3 = tk.Button(root, image=PlayerFour, command=lambda : pop_up3(root))
# Determine the sizes of each button
b0.pack(fill=X, ipady=20)
b1.pack(ipadx=15, side=LEFT, ipady=100)
b2.pack(ipadx=15, side=LEFT, ipady=100)
b3.pack(ipadx=15, side=LEFT, ipady=100)

root.mainloop()

First is that I created a UI using tkinter this is the image Click After that I initiate a program that uses a class.

def determine_players():
top = tk.Tk()
top.geometry("480x320")
text_file = open("players.txt", "r")
message = text_file.read()
players = message.split()
point1 = 0
point2 = 1
point3 = 2
point4 = 3
#print(players)
if len(players) == 2:

proc = ImageProcess()

Now, this is where the problem begins

class ImageProcess:
      def frame_table(self, image):
          if cell == '#':
                    def read_save():
                        blank_tile = entry_1.get()
                        blank_letter = blank_tile
                        text_file = open("blanktile.txt", "w")
                        text_file.write(blank_letter)
                        text_file.close()
                        f = open('blanktile.txt','r')
                        input_tile = f.read()
                        arr1[i][j] = input_tile
                        pop.destroy()

                    pop = tk.TK()
                    #root.geometry("200x100")                     
                    label_1 = tk.Label(pop,text = "Please input a letter for the blank tile")
                    label_1.pack()
                    entry_1 = tk.Entry(pop)
                    entry_1.pack(fill=X)
                    save_button = tk.Button(pop, text="Save",command=read_save)
                    save_button.pack(fill=X)

                    pop.mainloop()

When I try to create a pop-up message inside I keep on closing the entire tkinter. Can someone help me on how can I create a pop-up message in this particular code?

Xiao Sy
  • 1
  • 1
  • Read [Why are multiple instances of Tk discouraged?](https://stackoverflow.com/questions/48045401/why-are-multiple-instances-of-tk-discouraged) and [Is this bad programming practice in tkinter?](https://stackoverflow.com/a/25454433/7414759) – stovfl Jan 28 '19 at 18:52

1 Answers1

0

Instead of creating a Tk() to make the popup, create a Toplevel() (You will still have to call .mainloop() on the TopLevel()). To close this, call .destroy() on the Toplevel().

SomeMosa
  • 413
  • 5
  • 28
  • From here, you will just have to add widgets to your popup instead of your main program. (This will most likely not be an issue if you just change the variable `pop` to `tk.Toplevel()` and your widgets will be on your popup). When you close the popup, your main program will stay open unless you tell the program to close both. – SomeMosa Jan 28 '19 at 18:55