鱼C论坛

 找回密码
 立即注册
查看: 1358|回复: 1

[已解决]Toplevel 能否只建立一个窗口

[复制链接]
发表于 2020-6-29 09:50:49 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
各位大神 小甲鱼讲的tkinter这个东西,每次点击按钮都能弹出一个对话框,能否让他每次只独立弹出一个对话框。当第二个对话框打开时,前面那个对话框关闭呢?

from tkinter import *

root = Tk()

def create():
    top = Toplevel()
    top.title("FishC Demo")

    msg = Message(top, text="I love FishC.com!")
    msg.pack()

Button(root, text="创建顶级窗口", command=create).pack()

mainloop()

最佳答案
2020-6-29 10:26:46
本帖最后由 Twilight6 于 2020-6-29 10:35 编辑



这样即可:

  1. from tkinter import *

  2. root = Tk()


  3. def create():
  4.     if list_:
  5.         b = list_.pop()
  6.         b.destroy()

  7.     top = Toplevel()
  8.     top.title("FishC Demo")
  9.     list_.append(top)
  10.     msg = Message(top, text="I love FishC.com!")
  11.     msg.pack()


  12. list_ = []
  13. button = Button(root, text='创建顶级窗口', command=create)
  14. button.pack()

  15. mainloop()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-6-29 10:26:46 | 显示全部楼层    本楼为最佳答案   
本帖最后由 Twilight6 于 2020-6-29 10:35 编辑



这样即可:

  1. from tkinter import *

  2. root = Tk()


  3. def create():
  4.     if list_:
  5.         b = list_.pop()
  6.         b.destroy()

  7.     top = Toplevel()
  8.     top.title("FishC Demo")
  9.     list_.append(top)
  10.     msg = Message(top, text="I love FishC.com!")
  11.     msg.pack()


  12. list_ = []
  13. button = Button(root, text='创建顶级窗口', command=create)
  14. button.pack()

  15. mainloop()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-22 17:41

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表