Python,Tkinter 入口进入功能(错误来自按钮的功能调用)

Python, Tkinter entry get to function (error was with function call from button)

无论我做什么,入口值都不会传递到函数中。

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) 

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

这是完整的代码

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

您调用的不是入口函数,而是函数的输出。试试不带括号:

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)

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

变化:

    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 不要调用该函数,而只是 return 对它的引用,以后可以用来调用它。

执行此操作后,您会发现函数定义中有错字——您在 def 语句末尾缺少一个冒号。所以,改变:

def RegFunction(self, name)

至:

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