如何停止 while 循环以使用 Tkinter 从用户那里获取输入?

How to stop a while loop to get input from user using Tkinter?

令人难以置信的是,如此基础的知识竟然能埋藏在如此难得一见的地方。我花了将近一个月的时间在谷歌上搜索答案——观看视频、阅读文档、书籍等——关于如何停止 while 循环以使用 Tkinter 从用户那里获取输入。

我下面的代码循环了两次,然后在我能够提供输入之前就离开了循环!!!

无论如何,理论是值得赞赏的,但是示例代码会有更大的帮助。真的非常非常感谢。

# Python 3.5.1
from tkinter import *
from tkinter import ttk

loop = 0

while loop < 2:

    print ('loop')

    def user_data():
        user_input = data.get()
        print (user_input)

    lb=ttk.Label(root, text="Enter data")
    data=ttk.Entry(root)
    bt=ttk.Button(root, text='Ok', command=user_data)

    lb.grid(row=0, column=1)
    data.grid(row=0, column=2)
    bt.grid(row=0, column=3)

    loop += 1

print ('left loop')

root.mainloop()

好的,经过大量讨论,看来您实际上不需要等待任何东西。 GUI 处于等待事件的永久状态,因此您不需要添加额外的等待。您只需要一个 "save" 按钮,以及一种让用户输入可变数量值的方法。

要允许用户输入多个值,您可以添加一个按钮来添加更多输入小部件。唯一真正的技巧是保留一个条目小部件列表,以便您稍后可以在程序中获取值。

以下是说明该技术的工作示例。

注意:这不是我在真实 GUI 中实际执行此操作的方式,因为它依赖于全局变量。重点是说明动态添加入口widgets的一般原理。

import tkinter as tk

def add_entry():
    """
    Add two entries, one for a description, one for an amount,
    along with some labels
    """
    # create the label and entry widgets
    label1 = tk.Label(entry_frame, text="Description:")
    label2 = tk.Label(entry_frame, text="Amount:")
    entry1 = tk.Entry(entry_frame)
    entry2 = tk.Entry(entry_frame)

    # lay them out on the screen
    column, row = entry_frame.grid_size()
    label1.grid(row=row, column=0, sticky="e", pady=2)
    entry1.grid(row=row, column=1, sticky="ew", pady=2, padx=4)
    label2.grid(row=row, column=2, sticky="e", pady=2)
    entry2.grid(row=row, column=3, sticky="ew", pady=2, padx=4)
    entry_frame.grid_rowconfigure(row, weight=0)
    entry_frame.grid_rowconfigure(row+1, weight=1)

    # save the entries, so we can retrieve the values
    entries.append((entry1, entry2))

    # give focus to the new entry widget
    entry1.focus_set()

def save():
    # iterate over the entries, printing the values
    for description_entry, value_entry in entries:
        print("description: %s value: %s" % 
              (description_entry.get(), value_entry.get()))

# this is our global list of entries
entries = []

# create the main window and buttons
root = tk.Tk()

entry_frame = tk.Frame(root)
button_frame = tk.Frame(root)

entry_frame.pack(side="top", fill="both", expand=True)
button_frame.pack(side="bottom", fill="x")

add_button = tk.Button(button_frame, text="add another entry", command=add_entry)
save_button = tk.Button(button_frame, text="Save", command=save)

add_button.pack(side="left")
save_button.pack(side="right")

# create the first entry
add_entry()

# start the main loop -- this is where the GUI starts waiting,
# and why you don't need to add your own loop.
root.mainloop()

好吧,我觉得这个笑话毕竟是在我身上。 :-) 我花了三天时间告诉你这不容易做到,结果证明我错了。

我会尽快给你代码,让我解释一下我哪里错了。在我写最后一条评论时,我想向您解释的主要事情是,当您在 GUI 控制台中 运行 一个控制台 Python 程序时,这是两个不同的过程,并且事件循环GUI 与内部 Python 程序的工作方式无关。

在 GUI 应用程序中,它会崩溃,因为只有一个进程,并且它的事件循环不能同时 运行(响应正常的应用程序事件,例如重绘 window 或处理 clicks/keypresses) 并保持阻塞状态(等待您单击按钮)。我突然想到,如果 tkinter 能让用户创建额外的事件循环,事情就会变得非常简单。这不是不合理的:因为 tkinter 已经为自己的目的构造了一个,它只需要向用户公开该构造函数(或其他一些构造方法)。而且不会有任何 GIL 问题,因为另一个事件循环只是保持阻塞状态(实际上只有一个在执行)。

一旦我知道我在搜索什么,就不难找到了:你构造一个 Tk 变量(任何类型,布尔值很适合这种语义),切换它的值,然后在打回来。与此同时,你等待它改变。非常简单。 :-)

我试图尽可能少地更改您的代码,但是当然,一旦您了解了该机制,您就可以做得更好。我希望我已经回报了你对 tkinter 的信心。 :-)

from tkinter import *
from tkinter import ttk

root = Tk()
loop = 0
block = BooleanVar(root, False)

while loop < 2:

    print ('loop')

    def user_data():
        user_input = data.get()
        print (user_input)
        block.set(False)

    lb=ttk.Label(root, text="Enter data")
    data=ttk.Entry(root)
    bt=ttk.Button(root, text='Ok', command=user_data)

    lb.grid(row=0, column=1)
    data.grid(row=0, column=2)
    bt.grid(row=0, column=3)

    block.set(True)
    root.wait_variable(block)
    loop += 1

print ('left loop')
root.mainloop()