Search code examples
pythonuser-interfacetkinterwidgettkinter-entry

How to print Python Output in Entry widget?


I've searched through the whole Internet for how to do this, and nothing came to me. There were some similar topics, when programmers asked of how to parse 'int' numbers to the Entry output. But it is much simpler because you just use getters, then insert() - and voila.

I am trying to do the following thing. I print the text written in one line. And for each word I want to count how many times it appeared in the same text. E.g., I print in my first Entry "one two one two three" - I get "0 0 1 1 0" in the second Entry widget. Any non-space sequence of characters is considered a word.

from tkinter import *

class DM_3_1:

def __init__(self):
    root = Tk()
    root.geometry('250x150')
    root.title("DiscreteMaths_3_1")
    usertext = StringVar()

    Label_1 = Label(root, text="Input")
    Label_2 = Label(root, text="Output")
    inputField = Entry(root, textvariable = usertext)
    outputField = Entry(root)

    inputField.bind('<Return>', lambda _: printLine())

    def printLine():
        counter = {}
        for word in inputField.get():
            counter[word] = counter.get(word, 0) + 1
            Ans = print(counter[word] - 1, end=' ')
            outputField.insert(0, str(Ans))

    Label_1.grid(row = 0)
    Label_2.grid(row = 1)

    inputField.grid(row = 0, column = 1)
    outputField.grid(row = 1, column = 1)

    root.mainloop()

DM_3_1()

What I get in the output now: Here is the screenshot

As far as you can see, the application works, but there's 'NoneNoneNone...'(depends on the number of characters, including whitespaces) instead of '0 0 1 1 0'. How do I solve my problem? Where's a logical mistake? I guess, it's about the function, but I don't actually see the mistake.


Solution

  • You have set Ans to be equal to print rather than the value it was supposed to be. Also your for loop was getting every character rather than every word.

    Corrected code:

    from tkinter import *
    
    class DM_3_1:
    
        def __init__(self):
            root = Tk()
            root.geometry('250x150')
            root.title("DiscreteMaths_3_1")
            usertext = StringVar()
    
            Label_1 = Label(root, text="Input")
            Label_2 = Label(root, text="Output")
            inputField = Entry(root, textvariable = usertext)
            outputField = Entry(root)
    
            inputField.bind('<Return>', lambda _: printLine())
    
            def printLine():
                counter = {}
                words=inputField.get().split()
                for word in words:
                    counter[word] = counter.get(word, 0) + 1
                    Ans = counter[word] - 1
                    print(Ans, end=" ")
                    outputField.insert(END, str(Ans))
    
            Label_1.grid(row = 0)
            Label_2.grid(row = 1)
    
            inputField.grid(row = 0, column = 1)
            outputField.grid(row = 1, column = 1)
    
            root.mainloop()
    
    DM_3_1()
    

    edit:

    As Mike-SMT pointer out its easier to use .split

    code edited to use .split