繁体   English   中英

python,tkinter 条目到达 function(错误来自 F 按钮的 ZC1C425268E68385D14AB5074C17ZA49)

[英]python, tkinter entry get to function (error was with function call from button)

我在 8 小时内就能解决这个痛苦。 无论我做什么,入口值都不会传递到 function 中。 抱歉这个菜鸟问题

...

class Registration(tk.Frame):
    def __init__(self, parent, controller):

        name_var = tk.StringVar()

        name_entry = ttk.Entry(self, textvariable=name_var)
        name_entry.grid(row=1, column=1)

        button1 = ttk.Button(self, text="Registration", command=self.RegFunction(name_var.get()))
        button1.grid(row=4, column=4)

    def RegFunction(self, name)
        print(name) 

编辑:刚刚意识到,如果我向 function 添加一个变量,它会在我运行程序后立即被调用,并且它不关心按钮; 但如果我不给它一个变量,它就会正常工作,只有当我按下按钮时。 我一无所知。

这是整个代码

import tkinter as tk
from tkinter import ttk

class ToDoList(tk.Tk):

    def __init__(self, *args, **kwargs):

        tk.Tk.__init__(self, *args, **kwargs)


        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)

        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}


        for F in (LogIn, Registration):
            frame = F(container, self)

            self.frames[F] = frame

            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(LogIn)

    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()

class LogIn(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        # label of frame Layout 2
        label = ttk.Label(self, text="Log In")

        label.grid(row=0, column=4, padx=10, pady=10)

        username_label = ttk.Label(self, text="Username :")
        username_label.grid(row=1, column=1)

        password_label = ttk.Label(self, text="Password:")
        password_label.grid(row=2, column=1)

        usrname_entry = ttk.Entry(self)
        usrname_entry.grid(row=1, column=2)

        password_entry = ttk.Entry(self)
        password_entry.grid(row=2, column=2)


        button2 = ttk.Button(self, text="Registration",
                             command=lambda: controller.show_frame(Registration))


        button2.grid(row=4, column=1, padx=10, pady=10)


class Registration(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = ttk.Label(self, text="Registration")
        label.grid(row=0, column=4, padx=10, pady=10)

        name_label = ttk.Label(self, text="Username:")
        name_label.grid(row=1, column=0)
        pass1_label = ttk.Label(self, text="Password:")
        pass1_label.grid(row=2, column=0)
        pass2_label = ttk.Label(self, text="Password again:")
        pass2_label.grid(row=3, column=0)

        name_var = tk.StringVar()
        name_entry = ttk.Entry(self, textvariable=name_var)
        name_entry.grid(row=1, column=1)

        pass1_entry = ttk.Entry(self)
        pass1_entry.grid(row=2, column=1)
        pass2_entry = ttk.Entry(self)
        pass2_entry.grid(row=3, column=1)
        k = 12

        button1 = ttk.Button(self, text="Registration", command=self.RegFunction(k))

        button1.grid(row=4, column=4, padx=10, pady=10)

        button2 = ttk.Button(self, text="Back to Login",
                             command=lambda: controller.show_frame(LogIn))

        button2.grid(row=4, column=0, padx=10, pady=10)

    def RegFunction(self, x):
        print("Something", x)


app = ToDoList()
app.mainloop()

调用 function 和传入函数名称以便稍后调用它是有区别的。 ttk.Button command=期望传入一个 function 名称(或引用),以便稍后可以调用命名或引用的 function。 您正在调用 function 而不是传递其名称,所以 go 是错误的。

改变:

    button1 = ttk.Button(self, text="Registration", command=self.RegFunction(name_var.get()))

至:

    button1 = ttk.Button(self, text="Registration", command=lambda: self.RegFunction(name_var.get()))

你会更接近你的目标。 lambda告诉 Python 不要调用 function 而只是返回对它的引用,以便以后调用它。

一旦你这样做了,你会发现你的 function 定义中有一个错字——你在def语句的末尾缺少了一个冒号。 所以,改变:

def RegFunction(self, name)

至:

def RegFunction(self, name): # colon added at end

快乐编码!

您调用的不是 function 条目,而是 function 的结果。 不带括号试试:

button1 = ttk.Button(self, text="Registration", command=self.RegFunction(name_var))

此外,RegFunction 缺少冒号:

def RegFunction(self, name):
        print(name) 

command=self.RegFunction(name_var.get())将立即执行self.RegFunction() 您需要使用 lambda:

class Registration(tk.Frame):
    def __init__(self, parent, controller):
        super().__init__(parent)

        name_var = tk.StringVar()

        name_entry = ttk.Entry(self, textvariable=name_var)
        name_entry.grid(row=1, column=1)

        button1 = ttk.Button(self, text="Registration", command=lambda: self.RegFunction(name_var.get()))
        button1.grid(row=4, column=4)

    def RegFunction(self, name):
        print(name) 

请注意,您的代码没有在__init__()中调用super().__init__(parent) ) 。

暂无
暂无

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

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