【问题标题】:How to save the result of a button's command in tkinter?如何在 tkinter 中保存按钮命令的结果?
【发布时间】:2017-01-28 19:25:37
【问题描述】:
import tkinter as tk
from tkinter import simpledialog

def get_pass():

    user_password = simpledialog.askstring("Password Entry", "Enter your password here:")
    return user_password

submitButton = tk.Button(content, text="Start", command=get_pass)

#now I want to work with the password that the user entered.

用户应该点击“开始”按钮。单击按钮后,会出现一个 tkinter 消息框,要求用户输入密码。用户输入他们的密码并提交。根据上面的代码,密码返回一个名为 user_password 的字符串。

问题是,我如何使用用户输入的内容?按钮不保存函数的返回值。

【问题讨论】:

  • command= 无法检索到get_pass() 的结果,所以使用global 变量分配user_password
  • @furas 谢谢,这是有道理的。我是初学者,如果您向我展示了如何将变量设置为全局变量以便我可以在该函数之外使用它,我将不胜感激。
  • 在代码中查看我的答案和 cmets

标签: python user-interface tkinter


【解决方案1】:

command= 无法接收从get_pass() 返回的值,因此您可以使用global 变量(或StringVar)来分配user_password。或者直接在get_pass()中使用这个值来检查密码,你就不用回了。

import tkinter as tk
from tkinter import simpledialog

def get_pass():
    # inform function to use external/global variable when you use `=`
    global user_password

    # `user_password_var` is global variable too
    # but it doesn't need `global user_password_var` 
    # because it doesn't need `=` to assign value.

    result = simpledialog.askstring("Password Entry", "Enter your password here:")

    user_password = result
    # or
    user_password_var.set(result)
    # or
    if result != '123456':
        label['text'] = "ERROR: password is incorrect"
    else:
        label['text'] = "password is OK"

# --- main ---

root = tk.Tk()

# it creates global variable 
user_password = ''
# or
user_password_var = tk.StringVar()

label = tk.Label(root)
label.pack()

submitButton = tk.Button(root, text="Start", command=get_pass)
submitButton.pack()

# both variables are empty before `mainloop`

root.mainloop()

# both variables have value after `mainloop`

# print after you exit program
print(user_password)
print(user_password_var.get())

【讨论】:

  • 这是否意味着用户输入的值只有在我关闭窗口后才能打印?有没有办法在用户按下“提交”后立即打印值?
  • 不,这只是表示mainloop 启动程序并在您单击按钮时执行get_pass()mainloop 之前的窗口还不存在。
  • 你可以在get_pass()里面打印值,但是这个函数不会在mainloop()之前执行,而是在mianloop()里面执行
【解决方案2】:

首先您需要添加submitButton.pack()submitButton.grid() 以在tk 窗口中显示按钮。返回 user_password 不起作用,因为您没有将“命令”分配给密码。你可以把你的函数改成这样:

import tkinter as tk
from tkinter import simpledialog

def get_pass():
    global user_password
    user_password = simpledialog.askstring("Password Entry", "Enter your password here:")
    content.destroy()

content=tk.Tk()
submitButton = tk.Button(content, text="Start", command=get_pass)
submitButton.config(height=6, width=25, fg='red') #looks a little nicer
submitButton.pack()
content.mainloop()

#Do stuff with user_password
#print(user_password)

当您稍后打印 user_password 时,它将显示通过 simpledialog 输入的密码。密码现在是 user_password。

【讨论】:

  • 这似乎不起作用。当我在运行函数后编写 print(user_password) 时,pycharm 说“NameError: name 'user_password' is not defined”
猜你喜欢
  • 2022-11-30
  • 2020-12-15
  • 2021-06-04
  • 1970-01-01
  • 2017-01-25
  • 1970-01-01
  • 2021-04-14
  • 2023-02-24
  • 2019-07-31
相关资源
最近更新 更多