0
import tkinter as tk
from tkinter import ttk
import sqlite3

class Main(tk.Frame):
    def __init__(self, root):
        super().__init__(root)
        self.init_main()

    def init_main(self):

        toolbar = tk.Frame(bg='#d7d8e0', bd=2)
        toolbar.pack(side=tk.LEFT,fil=tk.Y)

        self.add_img = tk.PhotoImage(file='add.png')
        btn_open_dialog = tk.Button(toolbar,  text='Добавить', command = self.open_dialog, bg='#d7d8e0', bd = 0,
                                    compound = tk.TOP, image = self.add_img)
        btn_open_dialog.pack(side=tk.TOP)

        self.tree = ttk.Treeview(self, column=('ID','description','costs','total'), height=15 , show='headings')

        self.tree.column('ID', width=30, anchor=tk.CENTER)
        self.tree.column('description', width=300, anchor=tk.CENTER)
        self.tree.column('costs', width=150, anchor=tk.CENTER)
        self.tree.column('total',width=100, anchor=tk.CENTER)

        self.tree.heading('ID', text='№')
        self.tree.heading('description', text='Описание')
        self.tree.heading('costs', text='Сумма')
        self.tree.heading('total',text='Итог')

        self.tree.pack()

    def open_dialog(self):
        Child()


class Child(tk.Toplevel):
    def __init__(self):
        super().__init__(root)
        self.init_child()

    def init_child(self):

        def destroy_window(root):
            root.destroy()

        self.title('Доходы/Расходы')
        self.geometry('400x220+400+300')

        label_description = tk.Label(self, text='Описание: ')
        label_description.place(x=50, y=50)
        label_select = tk.Label(self, text='Доходы/Расходы: ')
        label_select.place(x=50, y=80)
        label_sum = tk.Label(self, text='Сумма: ')
        label_sum.place(x=50, y=110)


        self.entry_description = ttk.Entry(self)
        self.entry_description.place(x=200,y=50)
        self.entry_money = ttk.Entry(self)
        self.entry_money.place(x=200,y=110)

        self.combobox = ttk.Combobox(self, values=(u'Доходы',u'Расходы'),state='readonly')
        self.combobox.current(0)
        self.combobox.place(x=200,y=80)

        btn_add = tk.Button(self, text='Добавить')
        btn_add.place(x=220, y=170)
        btn_add.bind('<Button-1>')

        btn_cancel = tk.Button(self, text='Отменить')
        btn_cancel.place(x=300, y=170)
        btn_cancel.bind('<Button-1>')


        self.grab_set()
        self.focus_set()

1 ответ 1

3

При инициализации кнопки "Отменить":

btn_cancel = tk.Button(self, text='Отменить', command=lambda: self.destroy())
btn_cancel.place(x=300, y=170)

или

def destroy_window():
    self.destroy()

btn_cancel = tk.Button(self, text='Отменить', command=destroy_window)
btn_cancel.place(x=300, y=170)

или

def destroy_window(window):
    window.destroy()

btn_cancel = tk.Button(self, text='Отменить', command=lambda window=self: destroy_window(window))
btn_cancel.place(x=300, y=170)

Суть во всех случаях будет одна и та же.

Для кнопок лучше не делать привязку к событию <Button-1>. Когда я так делал, кнопка оставалась нажатой после отпускания мыши. Лучше делать обработку нажатия кнопки через параметр command при создании кнопки. Тем более что у вас btn_cancel.bind('<Button-1>') ничего не делает - при привязке обработчика к событию должна указываться функция-обработчик, примерно так:

btn_cancel.bind('<Button-1>', lambda event: self.destroy())

или

def destroy_window(event):
    self.destroy()

btn_cancel.bind('<Button-1>', destroy_window)

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.