Найти - Пользователи
Полная версия: Удаление кнопок на Python-е.
Начало » Центр помощи » Удаление кнопок на Python-е.
1
ken0i
Ребят, можете помочь. Не могу нормально удалить кнопки!
Притом,
 canvas.delete("all")
не помогает.
Удаляет только текст, но кнопки - нет.

Вот код, если что:
 from tkinter import *
import time
import random
import sys
tk = Tk()
r = random
s = sys
t = time
tk.title("Aqua-Drive")
tk.resizable(0, 0)
tk.wm_attributes("-topmost")
canvas = Canvas(tk, width="600", height="600", bg="aqua")
canvas.place(x=0, y=0)
canvas.pack()
def play_func():
    canvas.delete("all")
    top_text = text(275, 50, "Levels", "blue", 48)
def set_func():
    canvas.delete("all")
    top_text = text(275, 50, "Settings", "blue", 48)
      
def exit_func():
    canvas.delete("all")
    top_text = text(275, 50, "Do you really wanna exit?", "blue", 24)
    menu = [play_btn, set_btn, exit_btn]
    for x in range(0, 2):
        menu.pop(0)
    def yes_func():
        canvas.delete("all")
        top_text = text(275, 50, "Do you really wanna exit?", "blue", 24)
        s.exit()
    def no_func():
        canvas.delete("all")
        top_text = text(275, 50, "Do you really wanna exit?", "blue", 24)
    yes_btn = buttons("YES", yes_func, 0.25, 0.5, "white", "black", 64) 
    not_btn = buttons("NOT", no_func, 0.75, 0.5, "black", "white", 64)
def back_func():
    None
class buttons:
    def __init__(self, txt, func, x, y, bg, fg, ft):
        self = Button(tk, text=txt, command=func,
                    bd=5, background=bg,
                    foreground=fg,
                    font=ft).place(relx=x, rely=y)
class text:
    def __init__(self, x, y, txt, fg, fs):
        self = canvas.create_text(x, y, text=txt, fill=fg,
                                  font="Verdana %s" % fs)
top_text = text(275, 50, "Aqua-Drive", "blue", 48)
play_btn = buttons("PLAY", play_func, 0.375, 0.3525, "aqua", "dimgrey", 64)
set_btn = buttons("SETTINGS", set_func, 0.35, 0.5525, "aqua", "dimgrey", 64)
exit_btn = buttons("EXIT", exit_func, 0.3825, 0.7525, "aqua", "dimgrey", 64)
while 1:
    tk.update()
    tk.update_idletasks()
    t.sleep(0.01)
PEHDOM
ken0i
Ребят, можете помочь. Не могу нормально удалить кнопки!
это потому что canvas.delete удаляет только обьекты котороые созданы с помошью методов канваса, таких как create_polygon(), create_text(), create_blah-blah-blah() и тд.
если хотите поместить на кнавас виджет чтобы потом его можно было удалить делетом, используйте метод canvas.create_window()
http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/create_window.html
 from tkinter import *
root = Tk()
canvas = Canvas(root, width="100", height="100" )
canvas.pack()
b1 = Button(root, text='delete me', command=lambda: canvas.delete('all'))
canvas.create_window(30, 30, window=b1)
root.mainloop()
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB