简体   繁体   中英

how to return a value to the main function after clicking a button in tkinter?

In this file, I tried to return the value of employNum and employPass to the main function each time I clicked the display button. How can I do that?

from tkinter import *

def displayButton(root,employNum, employPass):

    Label(root,text = employNum.get() ).grid(row = 3, column = 1, sticky = N+S+W+E)
    Label(root, text = employPass.get()).grid(row = 4, column = 1, sticky = N+S+W+E)


def main():

    root = Tk()

    Label(root, text = 'Employee Number: ').grid(row = 0, column = 0, sticky = W)
    Label(root, text = 'Login Password: ').grid(row = 1, column = 0, sticky = W)

    employeeNum = StringVar()
    employeePass = StringVar()
    Entry(root, textvariable = employeeNum).grid(row = 0, column = 1, columnspan = 2, sticky = W)
    Entry(root, textvariable = employeePass).grid(row = 1, column = 1, columnspan = 2, sticky = W)

    checkButton = BooleanVar()
    Checkbutton(root, text = 'Remember Me', variable = checkButton).grid(row = 2, column = 1, sticky = W)

    Button(root, text = 'Save', relief = RAISED).grid(row = 2, column = 2, sticky = E)
    display = Button(root, text = 'Display', relief = RAISED, command = lambda: displayButton(root, employeeNum,employeePass))
    display.grid(row = 3, column = 2, sticky = E)

    Label(root, text = "Employee's number is ").grid(row = 3, column = 0, sticky = W)
    Label(root, text = "Employee's Passowrd is ").grid(row =4 , column = 0, sticky = W)
    root.mainloop()

main()

Button can't return value using return . You can only set value in global variable or passed as argument. You can change text in Label assigned to global variable or passed as argument. You can create new Label but root have to be global variable or passed as argument.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM