【发布时间】:2020-01-25 17:06:22
【问题描述】:
这是一段 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()
【问题讨论】:
-
我认为多线程是解决问题的方法,但我不知道如何在这种情况下应用它。 这太宽泛了。 Stack Overflow 并非旨在为您提供指南或教程。
标签: python python-3.x multithreading tkinter