python羊 发表于 2020-4-1 17:12:42

如何保持只有一个 子窗口

想要的效果:
无论第几次按下按钮,桌面 都只有一个按钮。
下面代码实现了部分功能,但是在用户主动关闭该子窗口的时候,按下按钮就不在显示子窗口了,但是temp列表值一直在增加。
————————————————————————————————————————————————————————————————————————————
from tkinter import *
root=Tk()
def top1():   
    top1=Toplevel()
    top1.title='弹窗1'

    temp.append(1)

    if len(temp)>1:
      top1.destroy()
      
    else:
      pass
    print(temp)      
   
temp=[]

button=Button(root,command=top1)
button.grid()


root.mainloop
————————————————————————————————————————————————————————————————————————————————————————————————————————

python羊 发表于 2020-4-1 17:30:58

?,你这个不是我的原代码吗?并未看到改动呀

一个账号 发表于 2020-4-1 17:35:02

python羊 发表于 2020-4-1 17:30
?,你这个不是我的原代码吗?并未看到改动呀

from tkinter import *
root=Tk()
def clear():
    global temp
    temp.clear()
    top1.destroy()
def top1():
    global top1
    top1=Toplevel()
    top1.title='弹窗1'
    top1.protocol("WM_DELETE_WINDOW", clear)

    temp.append(1)

    if len(temp)>1:
      top1.destroy()
      
    else:
      pass
    print(temp)      
   
temp=[]

button=Button(root,command=top1)
button.grid()


root.mainloop()

zltzlt 发表于 2020-4-1 17:39:37

from tkinter import *

root = Tk()


def top1():

    def clear():
      temp.clear()
      top1.destroy()

    top1 = Toplevel()
    top1.title = '弹窗1'
    top1.protocol("WM_DELETE_WINDOW", clear)    # 设置关闭窗口时要做的事

    temp.append(1)

    if len(temp) > 1:
      top1.destroy()
    else:
      pass
    print(temp)


temp = []

button = Button(root, command=top1)
button.grid()

root.mainloop()

python羊 发表于 2020-4-1 17:40:07

老哥,虽然点击删除之后,不会消失,但是程序会弹出多个子窗口。
我想用top1.state() 返回子窗口状态,当top1.state()不等于normal 的时候才新建窗口,但不知道怎么写这句话。

zltzlt 发表于 2020-4-1 17:42:13

python羊 发表于 2020-4-1 17:40
老哥,虽然点击删除之后,不会消失,但是程序会弹出多个子窗口。
我想用top1.state() 返回子窗口状态,当to ...

看看我的代码

一个账号 发表于 2020-4-1 17:47:18

python羊 发表于 2020-4-1 17:40
老哥,虽然点击删除之后,不会消失,但是程序会弹出多个子窗口。
我想用top1.state() 返回子窗口状态,当to ...

看看我的代码

python羊 发表于 2020-4-1 17:48:30

感谢老哥
页: [1]
查看完整版本: 如何保持只有一个 子窗口