0

I have a issue with tkinter. I want to use Entry to take info from the user and then compare it to a string if the result is true I want it to print a Label on the screen and if false I want it to print something else. For some reason the .get does not show for me, and I can't find a way to compare the user input in. The Entery to a simple string that I saved as a variable.

from tkinter import *

window = Tk()
window.geometry("800x600")
window.title("Sara's chocolate balls")

def Button_Click():
    test1 = "hey"
    if entry1 == test1:
        test2.pack()
    else:test3.pack()

entry1 = Entry(window, width=30).pack()

Button1 = Button(window, text="Button", command=Button_Click).pack()

test2 = Label(window, text="Good")
test3 = Label(window, text="Bad")

window.mainloop()
martineau
  • 119,623
  • 25
  • 170
  • 301
ido rozen
  • 35
  • 5
  • 2
    You *can't* do anything with the user input here, because you have no reference to the Entry - `entry1` is None, the result of applying `.pack()` to the Entry, rather than the Entry itself. You need to do the geometry management as a separate statement. And then you would need to use `entry1.get()` to actually retrieve the user input from the Entry; the Entry itself is not equal to anything. – jasonharper Jul 15 '22 at 14:24
  • So you say if i use .place or .grid i will be able to see .get to my entry1? – ido rozen Jul 15 '22 at 14:46
  • 1
    It's not whether you use .place, .grid, or .pack(), it's *how* you are using them. See [this answer](https://stackoverflow.com/a/1101765/355230) to a different tkinter question. – martineau Jul 15 '22 at 14:52
  • Thank you for trying to help , i changed it but still when i compare it it dose not work. – ido rozen Jul 15 '22 at 15:24

2 Answers2

0

So I found a solution:

entry1 = Entry(window,textvariable=var, width=30).pack()
var = StringVar()
user_input = var.get()

It works like that.

martineau
  • 119,623
  • 25
  • 170
  • 301
ido rozen
  • 35
  • 5
  • 1
    You're still setting `entry1` to `None` because that is what `pack()` always returns. It didn't cause harm in this code only because the variable is never referenced again due to the new use of a `StringVar`. – martineau Jul 15 '22 at 14:47
0

I want to use Entry to take info from the user and then compare it to a string if the result is true I want it to print a Label on the screen and if false I want it to print something else. For some reason the .get does not show for me, and I can't find a way to compare the user input in.

The problem can be fix.

Don't use the wildcard from tkinter import *. Use import tkinter as tk, then use prefix tk.

  • Use a Label widget, if you want to update, instead of creating a Label widget.
  • Split two lines instead of one line for Entry widget. to prevent error..AttributeError: 'NoneType' object has no attribute 'get'
  • Use .configure for Label widget updating.

Snippet re-script to improve readability:

import tkinter as tk

window = tk.Tk()
window.geometry("800x600")
window.title("Sara's chocolate balls")

def Button_Click():
    test1 = "hey"
    if entry1.get() == test1:
        test2.configure(text="Good")
    else:test2.configure(text="Bad")

entry1 = tk.Entry(window, width=30).pack()

Button1 = tk.Button(window, text="Button", command=Button_Click).pack()

test2 = tk.Label(window) 
test2.pack()
 

window.mainloop()

Screenshot:

enter image description here

toyota Supra
  • 3,181
  • 4
  • 15
  • 19