如何创建弹出式显示并单击按钮后关闭?

时间:2019-01-28 17:36:36

标签: python tkinter

我正在尝试制作游戏的UI,并且正在使用tkinter来创建此UI。我的问题是,如何在课程内部创建弹出式显示。这就是代码的工作方式。

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

首先,我使用tkinter创建了一个UI,这是图片Click,之后,我启动了一个使用类的程序。

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

现在,这是问题开始的地方

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

当我尝试在内部创建弹出消息时,我会继续关闭整个tkinter。有人可以帮助我如何用此特定代码创建弹出消息吗?

1 个答案:

答案 0 :(得分:0)

创建Tk()(而不是创建Toplevel()而不是弹出窗口)(您仍然必须在.mainloop()上调用TopLevel())。要关闭此窗口,请在.destroy()上调用Toplevel()