-1

Good evening, I am struggling to return an output without creating a button in Tkinker. I want to return either "Excellent" or "Done" based on the input but only the input is showing.

Below is the code I'm struggling with

from tkinter import *

root = Tk()

num = StringVar()
entry1 = Entry(root, textvariable=num).pack()

remark = StringVar()
entry2 = Entry(root, textvariable=remark).pack()

def set_label(name, index, mode):
    return remark.set(num.get())
    if result > 56:
        return "Excellent"
    else:
        return "Done"

num.trace('w', set_label)
num.set('')

root.mainloop
Jona
  • 1,218
  • 1
  • 10
  • 20
  • 5
    `if result > 56:...` is not executed. – Pedro Lobito Mar 01 '20 at 20:08
  • 1
    Return the output to whom or what? The `set_label()` function is going to be called by `tkinter` whenever `num` is changed, and it doesn't care what value is returned. I don't think you understand [event-driven programming](https://stackoverflow.com/questions/9342757/tkinter-executing-functions-over-time). – martineau Mar 01 '20 at 20:44
  • Also note that `pack()` always returns `None`, so that's the value you're assigning to `entry1` and `entry2`. – martineau Mar 01 '20 at 20:50
  • Everything was working with a button but I wanted to remove the button and that's where I got lost – billstone09 Mar 01 '20 at 21:51

1 Answers1

0

I was not exactly sure what you wanted to do, but I modified your function to determine if num entry is ''. If not then convert value fetched to int and compare to 56. If larger, then insert entry "Excellent" in remark entry, otherwise put "Done" in remark entry.

As you enter each digit, the comparison to 56 will take place so the first digit will always result in "Done" appearing. Once you exceed 56 (which will require a minimum of 2 digits) it will continue to remain "Excellent".

Again, I did the best I could with the logic provided. Here's the full code:

from tkinter import *

root = Tk()

num = StringVar()
entry1 = Entry(root, textvariable=num).pack()

remark = StringVar()
entry2 = Entry(root, textvariable=remark).pack()

def set_label(name, index, mode):
    result = num.get()
    if result == '':
        pass # not sure what rule should be here
    else:
        result = int(result)
        if result > 56:
            remark.set("Excellent")
        else:
            remark.set("Done")

num.trace('w', set_label)
num.set('')

root.mainloop
Scott
  • 367
  • 1
  • 9