【问题标题】:using multi-threading to execute a simple windowed program使用多线程执行一个简单的窗口程序
【发布时间】: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()

【问题讨论】:

标签: python python-3.x multithreading tkinter


【解决方案1】:

请注意,您在调用Tk 之前调用了函数input。这就是您的代码在显示窗口之前要求输入的原因。

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

当您使用“Tk”时,它将处理您使用input 的那种输入。 (你根本不需要那个。)

我建议您按照https://tkdocs.com/tutorial/ 的教程进行操作。在教程的几个屏幕中,您应该能够做您想做的事情。

祝你好运!

附录:

以教程页面https://tkdocs.com/tutorial/firstexample.html 中的 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()

【讨论】:

  • 我听不懂。仍然存在同样的问题,但仍然感谢您的帮助@Bill Bell
  • 查看更完整的示例。
猜你喜欢
  • 1970-01-01
  • 2023-03-29
  • 2018-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多