Hello everyone, I am writing a small program with an interface on Tkinter and I am faced with difficulty.

Conventionally, when a function is called (a click on the values ​​in Frame A), the conditional parameters are passed to the next frame (Frame B) and visualized, each time being updated in the Entry widget.

Depending on the value of the parameter passed (for example, "OMS"), you must provide the option to perform some other operation (function), and the button ("Do Smt") is drawn for this. But after selecting the next value, the button remains "hang" in frame B.

Tell me whether it is possible to somehow implement the check in the user_choice function of the presence of a button in Frame B and delete it. I understand that before each user_choice () call, I can delete all Frame B widgets and redraw them, but this solution seems like a crutch, I want to find a more delicate solution.

Sample code:

import tkinter as tk from tkinter import ttk class main_interface(): def __init__(self): self.window = tk.Tk() self.window.geometry("20x450") self.create_widgets() def create_widgets(self): self.window['padx'] = 10 self.window['pady'] = 10 entry = ttk.Entry(self.window, width=30) entry.grid(row=1, column=1, sticky=tk.W, pady=3) frame1 = ttk.LabelFrame(self.window, text="Frame A", relief=tk.RIDGE, width=200, height=200) frame1.grid(row=1, column=1, sticky=tk.E + tk.W + tk.N + tk.S) frame2 = ttk.LabelFrame(self.window, text="Frame B", relief=tk.RIDGE, width=200, height=200) frame2.grid(row=1, column=2, sticky=tk.E + tk.W + tk.N + tk.S) entry = ttk.Entry(frame2, width=30) entry.grid(row=1, column=1, sticky=tk.W, pady=3) request = {"NSK": "9383XXXX", "OMS": "9381XXXX", "TMN": "9345XXXX", "KZN": "9843XXXX", "NNV": "9831XXXX"} for row, (key, value) in enumerate(request.items()): formated_text = " {} - {}".format(key, value) found_result_label = ttk.Label(frame1, text=formated_text) selected_string = key, value found_result_label.bind("<Button-1>", func=lambda event, text=selected_string: user_choice(text)) found_result_label.grid(row=row, column=1, sticky=tk.W) def user_choice(text): entry.delete(0, "end") entry.insert(0, text) if text[0] == "OMS": button = ttk.Button(frame2, text="Do Smt", command=lambda: fx()) button.grid(row=2, column=1, sticky=tk.W) def fx(): pass program = main_interface() program.window.mainloop() 

    1 answer 1

    You can write a button in the field of the object, if necessary, delete it

      self.button = None def user_choice(text): entry.delete(0, "end") entry.insert(0, text) if self.button: # Если кнопка уже существует - self.button.destroy() # уничтожить!!! self.button = None if text[0] == "OMS": self.button = ttk.Button(frame2, text="Do Smt", command=lambda: fx()) self.button.grid(row=2, column=1, sticky=tk.W) 
    • Thank you for the interesting decision, but do not tell me such a nuance in your decision: suppose the button should appear when choosing 2 parameters (for example: if text [0] == "OMS" or text [0] == "NNV" :), then everything also works, but only until you select both suitable parameters in a row. Let me explain: choose "OMS" - a button appears, choose "NNV" - the button remains, then it does not matter what to choose - the button will remain with any combination. A bug or feature, why does this happen? - Luarvick
    • Yes, a bug. In this case, the old button is not deleted, and a new one is created on top of it. How do I get to the computer, update the answer. - insolor 2:41 pm
    • @Luarvick, corrected, just need to destroy the button (if it exists) when entering the function. - insolor
    • Thank you very much for your help - Luarvick