我正在尝试使用python和tkinter构建虚拟numpad(1、2和3)。你知道吗
如果我按1(按钮1),则帐户\号\项应更改为1,如果我按2,则帐户\号\项应相应更改。你知道吗
我试过这个密码,但是。你知道吗
import tkinter as tk
win = tk.Tk()
def handle_pin_button(event):
'''Function to add the number of the button clicked to the PIN number entry via its associated variable.'''
# Limit to 4 chars in length
# Set the new pin number on the pin_number_var
def create_login_screen():
accLabel = tk.Label(win, text="Acount Number / Pin")
accLabel.grid(row=1, column=0)
account_number_var = tk.StringVar()
account_number_entry = tk.Entry(win, textvariable=account_number_var)
account_number_entry.grid(row=1, column=1)
account_number_entry.focus_set()
pin_number_var = tk.StringVar()
account_pin_entry = tk.Entry(win, show='*', text='PIN Number', textvariable=pin_number_var)
account_pin_entry.grid(row=1, column=2)
# Buttons 1, 2 and 3 here. Buttons are bound to 'handle_pin_button' function via '<Button-1>' event.
button1 = tk.Button(text="1")
button1.bind('<Button-1>', handle_pin_button)
button1.grid(row=2, column=0, sticky="NESW")
button2 = tk.Button(text="2")
button2.bind('<Button-1>', handle_pin_button)
button2.grid(row=2, column=1, sticky="NESW")
button3 = tk.Button(text="3")
button3.bind('<Button-1>', handle_pin_button)
button3.grid(row=2, column=2, sticky="NESW")
create_login_screen()
tk.mainloop()
您需要在函数
create_login_screen()
之外创建帐号,否则当函数退出时它将被垃圾收集。你知道吗将按钮绑定到函数的常用方法是使用参数
command
。当您有一个函数处理来自多个按钮的回调时,通常的方法是使用lambda
。你知道吗示例如下:
如何使用
bind()
而不是按钮command
执行此操作:首先你需要跟踪按钮。因为它们是在函数中创建的,所以必须在全局范围中保存引用。你知道吗
其次,必须从列表索引计算按钮值,并相应地设置
account_number_var
。你知道吗然后将按钮绑定到回调处理程序。你知道吗
相关问题 更多 >
编程相关推荐