我正在尝试将事件click1绑定到自流4_2(这是一个标签),它不工作,无论我尝试。你知道吗
class Pagina_dos(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
texto4_1=tk.Label(self, text="Registro") .grid(row=0, column=0)
texto4_2=tk.Label(self, text="Peso") .grid(row=1, column=0)
entrada4_1=tk.Label(self, width=20, text=1)
entrada4_1.grid(row=0, column=1)
self.entrada4_2=ttk.Label(self,text="", width=20)
self.update_pesa()
def click1 (event=None):
global numero_registro
agarre=self.entrada4_2.cget("text")
lista_regsitro.append(agarre)
numero_registro=numero_registro+1
entrada4_1=tk.Label(self, width=20, text=numero_registro)
entrada4_1.grid(row=0, column=1)
self.entrada4_2.grid(row=1, column=1)
boton_registrar=tk.Button(self)
self.entrada4_2.bind('<Return>', click1)
boton_registrar.config(text="REGISTRAR",width=11, command=click1)
boton_registrar.grid(row=2, column=0)
boton_acabar=tk.Button(self, text="PARAR",width=7, command=controller.destruir).grid(row=2, column=1)
def update_pesa(self):
read=None
ser=serial.Serial("COM4",baudrate=9600)
while read==None:
Pesa=ser.read()
if Pesa==b"=":
read=ser.read(7)
self.entrada4_2.configure(text=read)
self.after(100, self.update_pesa)
当我执行代码时,按键不起作用,但按钮起作用。有没有办法将事件绑定到标签,或者将按键绑定到按钮?你知道吗
因为目标是让按下return键执行默认操作,所以最简单的解决方案是绑定到顶层本身。当你打开窗口时,你需要把焦点放在窗口上。根据操作系统的不同,默认情况下可能会发生这种情况,但最好明确地说明这一点。你知道吗
注意:当您绑定到
Toplevel
时,该Toplevel
中的所有小部件也会得到绑定,因此无论哪个内部小部件具有焦点,绑定都会工作。你知道吗相关问题 更多 >
编程相关推荐