单击tkinter中的按钮后如何将值返回到main函数?

时间:2015-12-02 02:59:13

标签: python events button tkinter

在此文件中,每次单击显示按钮时,我都会尝试将employNumemployPass的值返回到main函数。我怎么能这样做?

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()

1 个答案:

答案 0 :(得分:1)

按钮无法使用return返回值。您只能在全局变量中设置值或作为参数传递。您可以更改分配给全局变量的Label中的文字或作为参数传递。您可以创建新的Label,但root必须是全局变量或作为参数传递。