'Messagebox based on If Statement

solved!

    global entry
    label.config(text = entry.get())
    if entry == "" or " ":

needed to be:

    global entry
    entry_write1 = entry.get()
    entry_write = entry_write1.strip()
    label.config(text = entry_write)
    if entry_write == "" or entry_write == " ":

This way I can strip the whitespace from entry.get() so multiple spaces will bring the error box up too!

Thank you for the help!


Orginal Posting:

Using python here, and this was one of my assignments(and like, the first one I was able to do 95% by myself lmao).

I'm just having trouble getting my messagebox to stop popping up after every click of the button. I was using an if statement to try and get it to only pop up when there's no text. I know I'm missing something here!

I tried creating a little 'else:' with the label.config, but it wouldn't change the label and the messagebox would still pop up. Messing around with it further didn't seem to help either.

Here's my code:


#import everything and keeping an eye on messagebox
import tkinter as tk
from tkinter import messagebox

#make window
window = tk.Tk()

#create exit button that destroys window upon button press
ExitButton = tk.Button(text = "Exit", command = window.destroy, width = 2, height = 1, bg = "red2", fg = "ghost white")
ExitButton.pack()


#creating entry field and label, adding them to window
entry = tk.Entry()
label = tk.Label(text = "Enter Text Here: ")
label.pack()
entry.pack()

#function for changing label text in ExecuteButton
def changeLabelText():
    global entry
    label.config(text = entry.get())
    if entry == "" or " ":
        msg = messagebox.showerror("ERROR", "No Text To Display")

#creating execute button for command = ChangeLabelText and adding to window
ExecuteButton = tk.Button(text = "Execute", command = changeLabelText, width = 5, height = 1, bg = "forest green", fg = "gold",)
ExecuteButton.pack()


#create event loop
window.mainloop()

If anyone could help me refine the messagebox issue, that would be lovely.

Thank you!



Solution 1:[1]

Currently your code is:

label.config(text = entry.get())
if entry == "" or " ":

Why are you checking on entry if you are putting your information in the text variable?
Hence I would opt for:

label.config(text = entry.get())
if (text == "") or (text == " "):

As you see, checking if a variable equals one of the values is not done using:

var == value1 OR value2

But:

var == value1 OR var == value2

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dominique