简体   繁体   English

如何使用 tkinter 中的按钮将值从一个 function 返回到另一个

[英]How to return a value from one function to other using a button in tkinter

So, I am working on a project in which there is a login screen with option to type in the id and password, but then in the GUI I want that when the "Enter" button is clicked, it should return the id to the function where is is called.所以,我正在做一个项目,其中有一个登录屏幕,可以选择输入 id 和密码,但是在 GUI 中我希望当单击“Enter”按钮时,它应该将 id 返回到 function在哪里被称为。 My program is too long so I will give a small example to clear my point.我的程序太长了,所以我将举一个小例子来说明我的观点。

def func3():
       cred = func1()
       print(cred)
   def func1(): #Function for creating the GUI
       root = Tk()
       def func2(): #Function for getting the data and comparing with data from MySQL
           id = '123' #from entry widget
           flag = 0 
           # Comparison done: flag now equals 0 or 1
           if flag == 1: #When data matches
               return id
       btn = Button(master=root,text="Enter",command=func2) #This button should compare and return the id
       btn.pack()

               

From what I have understood, you are looking for something like this据我了解,您正在寻找这样的东西

from tkinter import *

def verify():
    global id_
    id_=id_entry.get()
    if id_==ID and pass_entry.get()==PASS:
        print('Success')
    else:
        print('Invalid')

root=Tk()

id_entry=Entry(root)
id_entry.pack()
pass_entry=Entry(root)
pass_entry.pack()
ent_button=Button(root,text='Enter',command=verify)
ent_button.pack()

ID='example'
PASS='password'

root.mainloop()

Using retrun will be pointless in this case since callbacks can't really return anything.在这种情况下使用retrun将毫无意义,因为回调不能真正返回任何东西。 It has been explained well in this post.这篇文章中已经很好地解释了。

UPDATE更新

If you declare the variable as global , you can access it from anywhere in the code如果将变量声明为global ,则可以从代码中的任何位置访问它

def verify():
    global id_
    id_=id_entry.get()
    if id_==ID and pass_entry.get()==PASS:
        success()
    else:
        print('Invalid')

def success():
    print(id_,'logged in successfully')

If you don't want to use global then you can pass it to the target function如果您不想使用global ,则可以将其传递给目标 function

def verify():
    id_=id_entry.get()
    if id_==ID and pass_entry.get()==PASS:
        success(id_)
    else:
        print('Invalid')

def success(id_):
    print(id_,'logged in successfully')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Python 中使用一个函数(方法)的返回值到另一个函数(方法) - Using a return value from one function(method) into other in Python 如何从从 tkinter 按钮执行的 function 返回值? - How can I return a value from an function that is executed from tkinter button? 单击tkinter中的按钮后,如何将值返回到主函数? - how to return a value to the main function after clicking a button in tkinter? 如何保存附加到 tkinter 按钮的函数的返回值? - How to save the return value of a function attached to a tkinter Button? 选择 tkinter 按钮后,如何从使用 filedialog 打开文件的 function 返回变量? - How can I return a variable from a function that is opening a file using filedialog after selecting a tkinter button? 如何使用Tkinter从一个函数的值返回另一个函数? - How to return a value from a function to another function with Tkinter? Tkinter:从一个 function 获取值并在另一个 function 中使用它 - Tkinter: Getting value from one function and using it in another function 不能从tkinter中的其他函数(def)获取单选按钮“值”,如何在不使用类的情况下实现这一点? - not getting radiobutton 'value' from other function(def) in tkinter, how to achieve this without using class? 使用列表框Tkinter中的值在按钮按下时调用函数 - Call a function on button press using value from listbox Tkinter 从tkinter按钮调用的函数返回数据 - return data from a function called by tkinter button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM