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

3
  • You should look into official documentation. docs.python.org/3/library/tkinter.ttk.html Commented May 25, 2020 at 23:35
  • Does this answer your question? How to get the Tkinter Label text? Commented May 26, 2020 at 8:29
  • I see all this, Nothing work this not entry text the is the display text Commented May 26, 2020 at 11:25

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Nothing work this not entry text the is the display text
@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?
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
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?
Yeh if lb empty don't deactivate button and if lb not empty active button
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.