【问题标题】:Python: Button's command event being called before any clickPython:在任何点击之前调用按钮的命令事件
【发布时间】:2019-10-07 18:23:18
【问题描述】:

我已阅读Why is the command bound to a Button or event executed when declared?,Bryan Oakley 提供的解决方案看起来很有用,但就我而言,我需要一些不同的东西(或者也许我错了)。由于我需要在MainFrame类中传递参数。

我的代码:

views/login.py

import tkinter as tk

from AgileBooks.controllers.login import submit_login

global_font = 'Helvetica'
global_bg = 'gray25'
global_fg = 'lawn green'


class MainFrame():
    # Frame:
    frm_login = tk.Tk()
    frm_login.title('Login | AgileBooks - Copyright Gonzalo Dambra')
    frm_login.geometry('400x300')
    frm_login.configure(background=global_bg)
    # Labels:
    lbl_username = tk.Label(frm_login, text='Username', bg=global_bg, fg=global_fg, font=(global_font, 16))
    lbl_username.place(x=150, y=50)
    lbl_password = tk.Label(frm_login, text='Password', bg=global_bg, fg=global_fg, font=(global_font, 16))
    lbl_password.place(x=150, y=125)
    # Inputtexts:
    txt_username = tk.Entry(frm_login, font=(global_font, 14))
    txt_username.focus()
    txt_username.place(x=100, y=80, height=25, width=200)
    txt_password = tk.Entry(frm_login, show='*',font=(global_font, 14))
    txt_password.place(x=100, y=155, height=25, width=200)
    # Button:
    btn_login = tk.Button(frm_login, text='Login', font=(global_font, 16), bg=global_bg, fg=global_fg,
                          command=submit_login(txt_username.get(), txt_password.get()))
    btn_login.place(x=165, y=200, height=25)


def main():
    frame = MainFrame()
    frame.frm_login.mainloop()


if __name__ == '__main__':
    main()

控制器/login.py:

def submit_login(username, password):
    if len(username) > 0 and len(password) > 0:
        print('Username: ', username, ' | Password: ', password)
    else:
        print('One of the fields is not filled.')

我的问题是 submit_login 方法在没有任何点击事件的情况下被调用,它只是在代码运行时被调用。 我做错了什么?

【问题讨论】:

  • 我看不出这个问题与您链接的问题有何不同。另一个问题为您提供了解决此问题所需的所有信息。

标签: python events button tkinter


【解决方案1】:

当您将submit_login 绑定到按钮时,您正在调用它:

command=submit_login(txt_username.get(), txt_password.get())

相反,在 Tkinter 中,您可以将命令绑定到 lambda:

command=lambda username=txt_username.get(), password=txt_password.get(): submit_login(username, password)

您可能还希望将您的呼叫转移到.get(),以便在点击时发生:

btn_login = tk.Button(frm_login, text='Login', font=(global_font, 16), bg=global_bg, fg=global_fg,    
                      command=lambda username=txt_username, password=txt_password: submit_login(username, password)


def submit_login(username, password):
    username = username.get()
    password = password.get()
    if len(username) > 0 and len(password) > 0:
        print('Username: ', username, ' | Password: ', password)
    else:
        print('One of the fields is not filled.')

【讨论】:

  • 成功了,谢谢。你对 .get() 的使用是对的。
  • @GonzaloDambra 关于你链接的问题也有很多很好的阅读,虽然这个问题可能应该因为重复而被关闭,但有时看到你的确切情况的直接答案会很有帮助.我建议通读 Bryan 的回答和他提供的链接。他知识渊博,我认为他的任何东西都非常有用。对于学习 Tkinter 的人来说,这是一个非常常见的痛点。
  • 更好的是在类本身中添加一个方法,以便您可以执行... command=self.login,然后登录方法可以获取数据而不是传递给它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-05
  • 2018-08-11
  • 2014-02-09
  • 1970-01-01
相关资源
最近更新 更多