简体   繁体   中英

How can I create a pop-up display and close after clicking a button?

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?

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() .

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