0

Hello guys I need to check if this label empty or not empty:

If not empty active some button.

I get the filename from another method.

this is the code :

lb = Label(self, text="",  background='white')
lb.config(text=excel_name)

enter image description here

john
  • 75
  • 1
  • 8
  • You should look into official documentation. https://docs.python.org/3/library/tkinter.ttk.html – Aman May 25 '20 at 23:35
  • Does this answer your question? [How to get the Tkinter Label text?](https://stackoverflow.com/a/6113152/7414759) – stovfl May 26 '20 at 08:29
  • I see all this, Nothing work this not entry text the is the display text – john May 26 '20 at 11:25

1 Answers1

1

Here is a simple app that demonstrates a button being activated or disabled based on whether a label's text is the empty string or not:

import tkinter as tk


class App(tk.Tk):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self.lb = tk.Label(self)
        self.lb.pack()

        self.entry = tk.Entry(self)
        self.entry.pack()

        self.button = tk.Button(self, text="A button", command=lambda: print("Button pressed"))
        self.button["state"] = tk.DISABLED
        self.button.pack()

        self.bind("<Return>", self.enter_pressed)

    def enter_pressed(self, event):
        self.lb.config(text=self.entry.get())

        self.button["state"] = tk.NORMAL if self.lb["text"] else tk.DISABLED


app = App()
app.mainloop()

The window contains a label, then a text entry box, then a button. If you type text into the text entry box then press the enter key (return key), the label text is set to the text in the text box, and the button is set to either enabled or disabled depending on if the label text is empty or not.

The key line here is:

self.button["state"] = tk.NORMAL if self.lb["text"] else tk.DISABLED

This sets the button state to either tk.NORMAL (enabled) or tk.DISABLED depending on whether the label text (self.lb["text"]) is the empty string or not.

Oli
  • 2,507
  • 1
  • 11
  • 23
  • Nothing work this not entry text the is the display text – john May 26 '20 at 11:25
  • @john I'm sorry, I don't understand your problem. You asked for how to get text from a label, and this code will do that for you. Maybe update your question with more detail? – Oli May 27 '20 at 13:17
  • Yes this code is give text from label but , I need to create function to know if this label is not empty click some button – john May 27 '20 at 13:42
  • Do you mean you want a function which, when you call it, simulates a button press if the label text is empty? e.g. if a have a label `lb` and a button `button`, if `lb` is empty, then do the command that is bound to `button`? – Oli May 27 '20 at 13:49
  • Yeh if lb empty don't deactivate button and if lb not empty active button – john May 27 '20 at 13:53
  • Edited. Is this what you wanted? – Oli May 27 '20 at 14:31
  • Yes I do it in in one function by this code if self.path: # if file is selected self.buAccepte['state'] = 'active' # enable button, I need to add # enable button in another function – john May 27 '20 at 14:42