-1

Here is a piece of python code, for some reason the code asks for input before it displays the window and I want the window to appear as soon as the program starts execution. I think that multithreading is the answer to it but I don't know how to apply it in this case. Here is my code:

from difflib import get_close_matches
from tkinter import *


data = {'Bob':'23', 'Sahil':'17', 'Swami':'34', 'Vaibhav':'21'}

def translate(w):
    w = w.lower()
    if w in data.keys():
        return data[w]
    elif w.title() in data.keys():
         return data[w.title()]
    elif w.upper() in data.keys():
         return data[w.upper()]
    elif len(get_close_matches(w, data.keys(), cutoff=0.8)) > 0:
        yn = input(
            "Did u mean %s instead? Enter y if yes, or any key if no :" % get_close_matches(w, data.keys(), cutoff=0.8)[
                0])
        if yn == 'y':
            return data[get_close_matches(w, data.keys(), cutoff=0.8)[0]]
        elif yn != 'y' and len(get_close_matches(w, data.keys(), cutoff=0.8)) > 1:
            yn2 = input("Did u mean %s instead? Enter y if yes, or any key if no :" %
                        get_close_matches(w, data.keys(), cutoff=0.8)[1])
            if yn2 == 'y':
                return data[get_close_matches(w, data.keys(), cutoff=0.8)[1]]
            elif yn2 != 'y' and len(get_close_matches(w, data.keys(), cutoff=0.8)) > 2:
                yn3 = input("Did u mean %s instead? Enter y if yes, or any key if no :" %
                            get_close_matches(w, data.keys(), cutoff=0.8)[2])
                if yn3 == 'y':
                    return data[get_close_matches(w, data.keys(), cutoff=0.8)[2]]
                elif yn3 != 'y':
                    return "The word doesn't exist in this dictionary"
            else:
                return "I cannot find this in dictionary"
        else:
            return "I cannot find this in dictionary"
    else:
        return "The word doesn't exist in this dictionary"
    try:
        t.insert(INSERT, word)

    except:
        t.insert(INSERT, "sorry")


while True:
    word = input("Please enter your word:")
    output = translate(word)
    if type(output) == list:
        for i in output:
            print(i)
    else:
        print(output)
    root = Tk()
    t = Text(root)
    t.pack()
    t.insert(INSERT, word)
    t.insert(INSERT, translate(word))
    root.mainloop()
Sahil 28
  • 1
  • 2
  • First you have to understand [Event-driven programming](https://stackoverflow.com/a/9343402/7414759). Read [Tkinter understanding mainloop](https://stackoverflow.com/questions/29158220/tkinter-understanding-mainloop), [Is this bad programming practice in tkinter?](https://stackoverflow.com/questions/25454065/is-this-bad-programming-practice-in-tkinter) and [Best way to structure a tkinter application](https://stackoverflow.com/a/17470842/7414759) – stovfl Jan 25 '20 at 19:13
  • _I think that multithreading is the answer to it but I don't know how to apply it in this case._ That is far too broad. Stack Overflow is not meant to provide you a guide or tutorial. – AMC Jan 25 '20 at 21:33

1 Answers1

0

Notice that you call the function input before you call Tk. That's why your code asks for input before it displays a window.

But there's something more basic that needs to be changed.

When you use 'Tk' it will handle input of the kind for which you are using input. (You won't need that at all.)

I would recommend that you follow the tutorial at https://tkdocs.com/tutorial/. Within a few screens of the tutorial you should be ablt to do what you want.

Best of luck!

Addendum:

Take the Python code from the tutorial page at https://tkdocs.com/tutorial/firstexample.html as a starting point. (Taking a working example as a place to start is often a good strategy.)

Simply replace identifiers and strings with character strings that make sense in your context. Then replace the code that does the actual calculation with the calculation you require. Here is a simplified code for your situation.

Notice especially that you use .get in Tk to obtain the contents of a field, and .set to put something into a display.

from tkinter import *
from tkinter import ttk
from difflib import get_close_matches

data = {'Bob':'23', 'Sahil':'17', 'Swami':'34', 'Vaibhav':'21'}

def Find(*args):
    try:
        value = word.get().lower().title()
        result.set(data[value])
    except ValueError:
        pass

root = Tk()
root.title("Example for Sahil")

mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)

word = StringVar()
result = StringVar()

word_entry = ttk.Entry(mainframe, width=7, textvariable=word)
word_entry.grid(column=2, row=1, sticky=(W, E))

ttk.Label(mainframe, textvariable=result).grid(column=2, row=2, sticky=(W, E))
ttk.Button(mainframe, text="Find", command=Find).grid(column=3, row=3, sticky=W)

ttk.Label(mainframe, text="word").grid(column=3, row=1, sticky=W)
ttk.Label(mainframe, text="is equivalent to").grid(column=1, row=2, sticky=E)
ttk.Label(mainframe, text="result").grid(column=3, row=2, sticky=W)

for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5)

word_entry.focus()
root.bind('<Return>', Find)

root.mainloop()
Bill Bell
  • 21,021
  • 5
  • 43
  • 58