Eu tenho uma IU do tkinter que tem um quadro, o quadro tem 2 telas, 1 tela tem um botão que cria uma janela de nível superior (chamada top). Essa janela de nível superior tem um botão CLOSE que fecha a janela superior (o que é feito facilmente por top.destoy). Mas, eu preciso do botão CLOSE para também chamar uma função que faz algo. Portanto, como o botão CLOSE não pode ser configurado para chamar something () e destroy (), configurei o botão para chamar sequence () que chama something () e top.destroy ().
Quando eu executo isso e clico no botão CLOSE, recebo o nome de erro 'top' não está definido. Eu sei por que está acontecendo, mas não sei como consertar isso. Alguma ideia?
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()
Use global top
dentro openApp1
e irá atribuir TopLevel
a variável global (em vez de variável local) e irá resolver o problema emsequence
def openApp1():
global top
c1.itemconfig(r, fill="red")
top = Toplevel()
Este artigo é coletado da Internet.
Se houver alguma infração, entre em [email protected] Delete.
deixe-me dizer algumas palavras