如何关闭由函数创建的新 windows
How to close a new windows that is created by a function
我有一个 tkinter UI,它有一个框架,框架有 2 个 canvases,1 个 canvas 有一个创建顶级的按钮 window (命名为顶部)。顶层 window 有一个关闭按钮,可以关闭顶层 window(top.destoy 很容易做到)。但是,我还需要 CLOSE 按钮来调用执行某些操作的函数。因此,由于无法将关闭按钮配置为调用 something() 和 destroy(),我将按钮设置为调用 sequence(),后者调用 something() 和 top.destroy()。
当我 运行 这并点击关闭按钮时,我收到错误名称 'top' 未定义。我知道为什么会这样,但我不知道如何解决这个问题。有什么想法吗?
import time
import tkinter as tk
import tkinter.font
from tkinter import*
window = Tk()
window.geometry("1920x1080")
window.title("HOME")
f1 = Frame (window, bg="white")
f1.pack()
c1 = Canvas(f1, height=200, width=1960, bg="white")
label = Label(f1, text="Running Apps", font= "Cambria 30 bold").pack()
c1.pack(anchor=N)
r = c1.create_rectangle(400, 50, 550, 150, fill="white", activefill="black")
r2 = c1.create_rectangle(550, 50, 700, 150, fill="white", activefill="black")
c2 = tk.Canvas(f1, height=800, width=1960, bg="white")
c2.pack(side="bottom")
def sequence():
top.destroy()
c1.itemconfig(r, fill="white") #something()
def openApp1():
c1.itemconfig(r, fill="red")
top = Toplevel()
top.geometry("1920x1080")
top.title("App 1")
cvs1 = tk.Canvas(top, height="880", width="800", bg="red")
Closebutton = Button(cvs1, text="CLOSE", command=sequence, padx="20", pady="0", justify="center", height="1", width="6", font="Cambria 20 bold", borderwidth="7")
cvs1.create_window(400, 600, window=Closebutton)
label1 = Label(top, text="I am App 1", font= "Cambria 50 bold")
label1.place(x=630, y=100)
cvs1.pack()
button1= Button(c2, bg="red", text="App 1", command=openApp1, padx="20", pady="10", justify="center", height="3", width="10", font="Cambria 30 bold", borderwidth="10")
c2.create_window(100, 200, anchor=NW, window=button1)
window.mainloop()
在openApp1
中使用global top
,它会将TopLevel
分配给全局变量(而不是局部变量),它会解决sequence
[=15=中的问题]
def openApp1():
global top
c1.itemconfig(r, fill="red")
top = Toplevel()
我有一个 tkinter UI,它有一个框架,框架有 2 个 canvases,1 个 canvas 有一个创建顶级的按钮 window (命名为顶部)。顶层 window 有一个关闭按钮,可以关闭顶层 window(top.destoy 很容易做到)。但是,我还需要 CLOSE 按钮来调用执行某些操作的函数。因此,由于无法将关闭按钮配置为调用 something() 和 destroy(),我将按钮设置为调用 sequence(),后者调用 something() 和 top.destroy()。
当我 运行 这并点击关闭按钮时,我收到错误名称 'top' 未定义。我知道为什么会这样,但我不知道如何解决这个问题。有什么想法吗?
import time
import tkinter as tk
import tkinter.font
from tkinter import*
window = Tk()
window.geometry("1920x1080")
window.title("HOME")
f1 = Frame (window, bg="white")
f1.pack()
c1 = Canvas(f1, height=200, width=1960, bg="white")
label = Label(f1, text="Running Apps", font= "Cambria 30 bold").pack()
c1.pack(anchor=N)
r = c1.create_rectangle(400, 50, 550, 150, fill="white", activefill="black")
r2 = c1.create_rectangle(550, 50, 700, 150, fill="white", activefill="black")
c2 = tk.Canvas(f1, height=800, width=1960, bg="white")
c2.pack(side="bottom")
def sequence():
top.destroy()
c1.itemconfig(r, fill="white") #something()
def openApp1():
c1.itemconfig(r, fill="red")
top = Toplevel()
top.geometry("1920x1080")
top.title("App 1")
cvs1 = tk.Canvas(top, height="880", width="800", bg="red")
Closebutton = Button(cvs1, text="CLOSE", command=sequence, padx="20", pady="0", justify="center", height="1", width="6", font="Cambria 20 bold", borderwidth="7")
cvs1.create_window(400, 600, window=Closebutton)
label1 = Label(top, text="I am App 1", font= "Cambria 50 bold")
label1.place(x=630, y=100)
cvs1.pack()
button1= Button(c2, bg="red", text="App 1", command=openApp1, padx="20", pady="10", justify="center", height="3", width="10", font="Cambria 30 bold", borderwidth="10")
c2.create_window(100, 200, anchor=NW, window=button1)
window.mainloop()
在openApp1
中使用global top
,它会将TopLevel
分配给全局变量(而不是局部变量),它会解决sequence
[=15=中的问题]
def openApp1():
global top
c1.itemconfig(r, fill="red")
top = Toplevel()