python - Tkinter 多个窗口未在按钮单击时打开
问题描述
单击按钮 clk 时,窗口的第一个实例打开,但在销毁第一个窗口后,后续窗口不会打开。只有在主实例 win 关闭后,后续窗口才会打开。
from tkinter import *
def func():
root = Tk()
b1 = Button(root,text='Click Me!').pack()
root.after(2000, lambda: root.destroy())
root.mainloop()
root=Tk()
b1 = Button(root,text='Click Me!',bg='orange').pack()
root.mainloop()
win = Tk()
clk = Button(win,text='func',command=func).pack()
win.mainloop()
没有语法错误,但我没有得到我想要的输出。
谢谢你
解决方案
就我的理解而言,只能有一个 mainloop()。我重写了您的代码以使其正常工作。
from tkinter import *
def func():
root = Toplevel()
b1 = Button(root,text='Click Me!').pack()
root.after(2000, lambda: second(root))
def second(root):
root.destroy()
root=Toplevel()
b1 = Button(root,text='Click Me!',bg='orange').pack()
win = Tk()
clk = Button(win,text='func',command=func).pack()
win.mainloop()
推荐阅读
- node.js - 何时在 MongoDB 中使用嵌入与引用
- android - 主屏幕上自动出现多个灰色图标
- python - 使用 pandas loc 时进行索引
- python - Python dict 的工作方式类似于 oderedDict?
- json - 在 Grails 中以 JSON 格式渲染复杂对象
- python - python代码根据给定条件查找素数或合数
- mongodb - 即使我连接并使用该数据库,Mongo shell 也会抛出“ReferenceError db is not defined”?
- asp.net - ASP.NET 返回响应并继续执行
- java - 导出到 Excel 时 ADF 复选框列显示值而不是标签
- python - 我正在尝试处理文本文件,但我不断将变量分配给错误的数据