简体   繁体   English

如何从 function 给 main 赋值

[英]How to give value to main from function

In my program there is a main part with a windows options and I need to go to Greetings def.在我的程序中有一个带有 windows 选项的主要部分,我需要 go 到 Greetings def。

if __name__ == "__main__":
    web = Tk()

    web.configure(background = "black")

    web.title("#######")

    web.geometry("600x400")

    web.iconbitmap('icon.ico')

    web.resizable(False, False)

    web.protocol("WM_DELETE_WINDOW", close)

    Greetings(log)

    def Greetings(log):
        
        log.withdraw()
        def clear_form():
            greetings_entry_name.delete(0, END)
            greetings_entry_password.delete(0, END)
        greetings_frame = Frame(web, background = "black", pady = 10)
        greetings_frame.pack()

But I have another def with another window (login) and I need the 'log' for giving permission to hide login window.但是我有另一个 def 和另一个 window(登录),我需要“日志”来授予隐藏登录 window 的权限。

def login():
        web.withdraw()
        log = Tk()
        ...
        ...
        registration_back_button = Button(login_frame, text = "Go back", bg = "#293133", fg = "white", command = Greetings(log))
        registration_back_button.pack(pady = 15)
        return log

So if i want to compilate i have predictable error所以如果我想编译我有可预测的错误

name 'log' is not defined名称“日志”未定义

How can I give log to Greetings or maybe don't do it this way.我怎样才能给 Greetings 日志或者不这样做。

I am not sure if this is what you are looking for, but you can use nested functions, so your Toplevel window is defined.我不确定这是否是您要查找的内容,但您可以使用嵌套函数,因此您的Toplevel window 已定义。 You cannot use return together with a button.您不能将return与按钮一起使用。 Your code is not reproducible, so I created an example with a login window. I added grab_set() to avoid that the login window can be ignored.您的代码不可重现,因此我创建了一个登录名 window 的示例。我添加了grab_set()以避免可以忽略登录名 window。 Also I used destroy() instead of withdraw() .我还使用了destroy()而不是withdraw() Password: You shall not pass密码:你不得通过

import tkinter as tk

window = tk.Tk()


def login():
    log = tk.Toplevel(window)
    log.grab_set()

    def check_pw():
        password = entry1.get()

        if password == 'You shall not pass':
            label1 = tk.Label(window, text='Login successful!')
            label1.pack()
            button1.destroy()
            log.destroy()
        else:
            label2 = tk.Label(log, text='Wrong password')
            label2.pack()

    entry1 = tk.Entry(log)
    entry1.pack()

    button2 = tk.Button(log, command=check_pw, text='Login')
    button2.pack()


button1 = tk.Button(window, command=login, text='Press for login')
button1.pack()

window.mainloop()

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

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