Python Forum
[Tkinter] Hide clicked buttons
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Hide clicked buttons
#1

.csv   PostDummy.csv (Size: 481 bytes / Downloads: 300)
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
Reply
#2
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()
Reply
#3
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

Attached Files

.csv   PostDummy.csv (Size: 481 bytes / Downloads: 102)
Reply
#4
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()
Reply
#5
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.
Reply
#6
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()
Reply
#7
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] Button clicked not working catlessness 10 8,083 Oct-21-2021, 12:36 PM
Last Post: catlessness
  [tkinter] not getting checkbutton value when clicked OogieM 5 5,882 Sep-20-2020, 04:49 PM
Last Post: deanhystad
  [PyQt] Avoid clicked event from button when button is not physically selected and clicked mart79 2 2,309 May-05-2020, 12:54 PM
Last Post: mart79
  [Tkinter] Displaying Data from a database and run a function when clicked? PythonNPC 1 2,022 Mar-11-2020, 08:16 PM
Last Post: Larz60+
  tkinter button not accessing the command when clicked jhf2 1 3,500 Nov-23-2019, 10:17 PM
Last Post: DT2000
  [Tkinter] How to check after 30 minutes if Buttons have been clicked? bmghafoor 1 2,076 Aug-09-2019, 04:57 PM
Last Post: Yoriz
  [PyQt] Hide Dock Icon for QSystemTrayIcon App AeglosGreeenleaf 0 3,264 Jun-20-2019, 07:21 PM
Last Post: AeglosGreeenleaf
  General help with hide show status bar for a begineer in python ArakelTheDragon 0 2,751 Mar-17-2019, 11:58 AM
Last Post: ArakelTheDragon
  Hide button when clicked frequency 2 8,691 Dec-24-2018, 02:10 PM
Last Post: frequency
  [PyGUI] Hi All, how to hide/mask user input in PySimpleGUI nmrt 1 14,836 Sep-21-2018, 09:59 AM
Last Post: nmrt

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020