Не получается вызвать функцию при нажатии на кнопку

Рейтинг: 1Ответов: 1Опубликовано: 18.01.2023

Пишу приложение. Не получается при нажатии на кнопку вызвать функцию c помощью command=lambda.
Хотя ранее пользовался данным способом и все работало.

ОШИБКА:

NameError: free variable 'destroy_start' referenced before assignment in enclosing scope

from tkinter import *

def profile_window(): #функция для личного профиля
    home_window = Tk()  # создание домашней страницы личного профиля
    home_window.title("Добро пожаловать!")
    home_window.geometry("900x700")
    home_window.resizable(width=FALSE, height=FALSE)
    home_window['bg'] = 'white'
    image = PhotoImage(
        file=r"/home/arkadi/PycharmProjects/recomendation-book-system/resource/image/perepis.png")
    user_avatar = Label(home_window, image=image)  # место аватарки для пользователя
    user_avatar.place(x=20, y=20)
    button_input = Button(home_window, text='Выбрать фотку', bg='gold', font='Arial 9')#, command=lambda: #open_file())
    button_input.place(x=75, y=280)
    avatar_name = Label(home_window, text='Возраст', font='Arial 18', bg='gold', fg='black', padx=30)
    avatar_name.place(x=300, y=30)
    text_login = Label(home_window, text='Введенное имя', font='Arial 18', bg='gold', fg='black', padx=30)
    text_login.place(x=300, y=100)
    **button_input1 = Button(home_window, text='Выберите жанр книги!', bg='gold', font='Arial 13', height=1, command=lambda:destroy_start())
    button_input1.place(x=300, y=200)**
    button_input2 = Button(home_window, text='Выберите жанр книги!', bg='gold', font='Arial 13', height=1)
    button_input2.place(x=300, y=250)
    button_input2 = Button(home_window, text='Выберите жанр книги!', bg='gold', font='Arial 13', height=1)
    button_input2.place(x=300, y=300)
    button_input4 = Button(home_window, text='Выберите жанр книги!', bg='gold', font='Arial 13', height=1)
    button_input4.place(x=550, y=200)
    button_input4 = Button(home_window, text='Выберите жанр книги!', bg='gold', font='Arial 13', height=1)
    button_input4.place(x=550, y=250)
    button_input6 = Button(home_window, text='Выберите жанр книги!', bg='gold', font='Arial 13', height=1)
    button_input6.place(x=550, y=300)
    button_forgot = Button(home_window, text='Подобрать книги', bg='gold', font='Arial 18')
    button_forgot.place(x=420, y=350)
    home_window.mainloop()
    def destroy_start():
       home_window.destroy()



profile_window()

Ответы

▲ 1Принят

Попробуйте так:

from tkinter import *

def profile_window():   
    home_window = Tk()  
    home_window.title("Добро пожаловать!")
    home_window.geometry("900x700")
    home_window.resizable(width=FALSE, height=FALSE)
    home_window['bg'] = 'white'
    
    def destroy_start():                                      # <----
        print(f'def destroy_start(): ')                       # <----
        home_window.destroy()                                 # <----

    image = PhotoImage(
        file=r"Ok.png")                                  # установите свое

    user_avatar = Label(home_window, image=image)  
    user_avatar.place(x=20, y=20)
    button_input = Button(home_window, text='Выбрать фотку', bg='gold', font='Arial 9')#, command=lambda: #open_file())
    button_input.place(x=75, y=280)
    avatar_name = Label(home_window, text='Возраст', font='Arial 18', bg='gold', fg='black', padx=30)
    avatar_name.place(x=300, y=30)
    text_login = Label(home_window, text='Введенное имя', font='Arial 18', bg='gold', fg='black', padx=30)
    text_login.place(x=300, y=100)
    
    
    button_input1 = Button(home_window, 
        text='Выберите жанр книги!', 
        bg='gold', font='Arial 13', height=1, 
        command=lambda: destroy_start())
    button_input1.place(x=300, y=200)
    
    button_input2 = Button(home_window, text='Выберите жанр книги!', bg='gold', font='Arial 13', height=1)
    button_input2.place(x=300, y=250)
    button_input2 = Button(home_window, text='Выберите жанр книги!', bg='gold', font='Arial 13', height=1)
    button_input2.place(x=300, y=300)
    button_input4 = Button(home_window, text='Выберите жанр книги!', bg='gold', font='Arial 13', height=1)
    button_input4.place(x=550, y=200)
    button_input4 = Button(home_window, text='Выберите жанр книги!', bg='gold', font='Arial 13', height=1)
    button_input4.place(x=550, y=250)
    button_input6 = Button(home_window, text='Выберите жанр книги!', bg='gold', font='Arial 13', height=1)
    button_input6.place(x=550, y=300)
    button_forgot = Button(home_window, text='Подобрать книги', bg='gold', font='Arial 18')
    button_forgot.place(x=420, y=350)
    home_window.mainloop()


profile_window()

введите сюда описание изображения