Python 使用多线程执行简单的窗口程序

Python 使用多线程执行简单的窗口程序,python,python-3.x,multithreading,tkinter,Python,Python 3.x,Multithreading,Tkinter,这是一段python代码,出于某种原因,代码在显示窗口之前要求输入,我希望窗口在程序开始执行时立即出现。 我认为多线程是解决这个问题的办法,但我不知道如何在这种情况下应用它。 这是我的密码: from difflib import get_close_matches from tkinter import * data = {'Bob':'23', 'Sahil':'17', 'Swami':'34', 'Vaibhav':'21'} def translate(w): w = w

这是一段python代码,出于某种原因,代码在显示窗口之前要求输入,我希望窗口在程序开始执行时立即出现。 我认为多线程是解决这个问题的办法,但我不知道如何在这种情况下应用它。 这是我的密码:

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()

请注意,在调用
Tk
之前,先调用函数
input
。这就是为什么代码在显示窗口之前要求输入

但还有一些更基本的东西需要改变

当您使用“Tk”时,它将处理您正在使用的输入类型。(你根本不需要这个。)

我建议您在上学习本教程。在本教程的几个屏幕中,您应该能够做您想做的事情

祝你好运

增编:

以处的教程页面中的Python代码为起点。(将工作实例作为起点通常是一个好策略。)

只需将标识符和字符串替换为在上下文中有意义的字符串。然后将执行实际计算的代码替换为所需的计算。下面是一个针对您的情况的简化代码

特别要注意的是,在Tk中使用
.get
获取字段的内容,使用
.set
将内容放入显示器

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()
从tkinter导入*
从tkinter导入ttk
从difflib导入获取\u关闭\u匹配
数据={'Bob':'23','Sahil':'17','Swami':'34','Vaibhav':'21'}
def Find(*args):
尝试:
value=word.get().lower().title()
result.set(数据[值])
除值错误外:
通过
root=Tk()
root.title(“Sahil示例”)
mainframe=ttk.Frame(根,padding=“3 3 12”)
grid(列=0,行=0,粘性=(N,W,E,S))
root.columnconfigure(0,权重=1)
rowconfigure(0,权重=1)
word=StringVar()
结果=StringVar()
word_entry=ttk.entry(大型机,宽度=7,文本变量=word)
word_entry.grid(列=2,行=1,粘滞=(W,E))
标签(大型机,textvariable=result).grid(列=2,行=2,粘性=(W,E))
ttk.按钮(大型机,text=“Find”,command=Find).grid(列=3,行=3,粘性=W)
ttk.Label(mainframe,text=“word”).grid(column=3,row=1,sticky=W)
ttk.Label(mainframe,text=“相当于”).grid(column=1,row=2,sticky=E)
ttk.Label(mainframe,text=“result”).grid(column=3,row=2,sticky=W)
对于mainframe.winfo_children():child.grid_配置(padx=5,pady=5)
word_entry.focus()
root.bind(“”,Find)
root.mainloop()

首先你必须理解。阅读,我认为多线程是解决这个问题的答案,但我不知道如何在这种情况下应用它。这太宽泛了。堆栈溢出不是为了给你提供指导或教程,我搞不懂。仍然存在相同的问题,但仍然感谢您的帮助@Bill Bellse查看更完整的示例。