Python Forum
[Tkinter] Hide clicked buttons - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: [Tkinter] Hide clicked buttons (/thread-33845.html)



Hide clicked buttons - Rubberduck - Jun-01-2021

[attachment=1114]
Hi There

I already code a small app which fulfils my needs. Now, I want to hide the clicked buttons from the upper part of my app. Has anyone a good solution for this need? My code looks as following:

from tkinter import *
from PIL import ImageTk, Image
import csv


root = Tk()
root.title('Fahrzeugkontrolle')
root.geometry('500x1000')
#root.iconbitmap('bus.png')

kfz = StringVar()
kfz.set("prove")

with open('PostDummy.csv', 'r') as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')

    next(csv_reader)
    
    for line in csv_reader:
        x = (line[3])
        y = (line[1])
        z = (x + ",   " + y)
        Radiobutton(root, text=x, variable=kfz, value=z).pack(anchor=W)


def clicked(value):
    myLabel = Label(root, text=value)
    myLabel.pack()

myLabel = Label(root, text=kfz.get())
myLabel.pack()

myButton = Button(root, text="OK", command=lambda: clicked(kfz.get()))
myButton.pack()

root.mainloop()
Thanks for your help,
Rubberduck


RE: Hide clicked buttons - deanhystad - Jun-01-2021

When posting try to avoid any uncommon external dependencies, like a csv file that only you have.

You can remove buttons using pack_forget.
from tkinter import * 
 
root = Tk()
buttons = {}

def clicked(value):
    myLabel = Label(root, text=str(value))
    myLabel.pack()
    buttons[value].pack_forget()

kfz = IntVar()
kfz.set(0)
kfz.trace_add('write', lambda a, b, c: clicked(kfz.get()))
 
for x, z in {'these':1, 'are':2, 'the':3, 'choices':4}.items():
    button = Radiobutton(root, text=x, variable=kfz, value=z)
    buttons[z] = button
    button.pack(anchor=W)
  
root.mainloop()



RE: Hide clicked buttons - Rubberduck - Jun-01-2021

Thanks a lot @deanhystad

Sorry, I have forgotten adding the csv. Now it is attached.

Your code looks nice, but I would like to only hide the values stored in x so that they disappear from the upper part of the app but then the values stored in z should stay in the second half of the app. Furthermore I would like that the x values only disappear after the user has clicked on the "OK" Button.

Do you have a good solution for this?

Many thanks,
Rubberduck


RE: Hide clicked buttons - Yoriz - Jun-01-2021

Don't use from tkinter import * see Namespace flooding with * imports
If you don't want the widget anymore you can destroy it.
import tkinter as tk
import csv


def yield_csv(file_path):
    with open(file_path, 'r') as csv_file:
        csv_reader = csv.reader(csv_file, delimiter=',')
        for line in csv_reader:
            yield line


class App(tk.Tk):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.title('Fahrzeugkontrolle')
        self.geometry('500x1000')
        self.string_var = tk.StringVar()
        self.string_var.set("prove")
        self.r_btns = {}

        csv_ = yield_csv('PostDummy.csv')
        next(csv_)
        for line in csv_:
            value = f'{line[3]}, {line[1]}'
            r_btn = tk.Radiobutton(self, text=line[3],
                                   variable=self.string_var, value=value)
            r_btn.pack(anchor=tk.W)
            self.r_btns[value] = r_btn

        myLabel = tk.Label(self, text=self.string_var.get())
        myLabel.pack()

        myButton = tk.Button(self, text="OK", command=self.on_btn)
        myButton.pack()

    def on_btn(self):
        value = self.string_var.get()
        myLabel = tk.Label(self, text=value)
        myLabel.pack()
        self.r_btns[value].destroy()


if __name__ == '__main__':
    app = App()
    app.mainloop()



RE: Hide clicked buttons - deanhystad - Jun-01-2021

A csv file is not required to demonstrate your problem. I used a dictionary instead. The csv file is an unimportant implementation detail that is not germane to your question. When posting code write your example to only import a module or use an external resource if it is absolutely required. If you want help, make it easy for others to help you.

If you want the OK button to call "clicked", then have the OK button call "clicked". Just thought you might be interested in seeing how kfz could be used to do the job without requiring an extra button press.


RE: Hide clicked buttons - Rubberduck - Jun-02-2021

Hi Yoriz

Awesome. Thanks a lot for your excellent response and also for the good input about the from tkinter import *. Your code works absolutely perfect and fulfils my needs.

Best Wishes,
Rubberduck

(Jun-01-2021, 06:29 PM)Yoriz Wrote: Don't use from tkinter import * see Namespace flooding with * imports
If you don't want the widget anymore you can destroy it.
import tkinter as tk
import csv


def yield_csv(file_path):
    with open(file_path, 'r') as csv_file:
        csv_reader = csv.reader(csv_file, delimiter=',')
        for line in csv_reader:
            yield line


class App(tk.Tk):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.title('Fahrzeugkontrolle')
        self.geometry('500x1000')
        self.string_var = tk.StringVar()
        self.string_var.set("prove")
        self.r_btns = {}

        csv_ = yield_csv('PostDummy.csv')
        next(csv_)
        for line in csv_:
            value = f'{line[3]}, {line[1]}'
            r_btn = tk.Radiobutton(self, text=line[3],
                                   variable=self.string_var, value=value)
            r_btn.pack(anchor=tk.W)
            self.r_btns[value] = r_btn

        myLabel = tk.Label(self, text=self.string_var.get())
        myLabel.pack()

        myButton = tk.Button(self, text="OK", command=self.on_btn)
        myButton.pack()

    def on_btn(self):
        value = self.string_var.get()
        myLabel = tk.Label(self, text=value)
        myLabel.pack()
        self.r_btns[value].destroy()


if __name__ == '__main__':
    app = App()
    app.mainloop()



RE: Hide clicked buttons - Rubberduck - Jun-02-2021

Hi @deanhystad

Thank you very much for the additional information. Your solution is of course a great one too but the stepp with an extra button should ensure that the user really wants to tick off the task. Especially because most of the users will be confronted for the first time with a digital solution for this process.

Best Wishes,
Rubberduck

(Jun-01-2021, 07:13 PM)deanhystad Wrote: A csv file is not required to demonstrate your problem. I used a dictionary instead. The csv file is an unimportant implementation detail that is not germane to your question. When posting code write your example to only import a module or use an external resource if it is absolutely required. If you want help, make it easy for others to help you.

If you want the OK button to call "clicked", then have the OK button call "clicked". Just thought you might be interested in seeing how kfz could be used to do the job without requiring an extra button press.