0
我相信这段代码是关闭的 - 我有三个按钮用回调定义,用于存储与所选按钮关联的IntVar()变量。我创建了一个临时变量(cntr),用于存储相应的Intvar。这似乎是基于我在回调中的打印声明工作。但是当我选择其中一个按钮时,点击在代码末尾配置的inc或dec按钮,唯一改变的“entry”按钮就是在代码的主要部分中设置的那个按钮。谁能告诉我什么我缺少使用相同的2个控制按钮控制多个输入按钮
#!/usr/bin/python3
from tkinter import *
from tkinter import PhotoImage
def increment (cntr):
print('in inc', cntr)
cntr.set("{0:0>4}".format(cntr.get() + 1))
def decrement (cntr):
print('in dec', cntr)
cntr.set("{0:0>4}".format(cntr.get() - 1))
def EntryCB(arg):
global cntr
cntr = arg
print('in EntryCB', cntr, arg)
root = Tk()
root.configure(bg='white')
frame1 = LabelFrame(root, text="Component 1",)
frame1.grid()
#UpArrow = PhotoImage(file="images//UpArrow.png")
#DownArrow = PhotoImage(file="images/DownArrow.png")
counter1 = IntVar()
counter1.set("{0:0>4}".format(0))
counter2 = IntVar()
counter2.set("{0:0>4}".format(0))
counter3 = IntVar()
counter3.set("{0:0>4}".format(0))
cntr=counter1
print('in main', cntr)
#setup entry buttons tied to IntVar variables
Entry1 = Button(frame1, textvariable=counter1)
Entry1.grid(row=0, column=0)
Entry1.config(command= lambda arg=counter1:EntryCB(arg))
Entry1.configure(borderwidth=3, relief='sunken', bg='green')
Entry2 = Button(frame1, textvariable=counter2)
Entry2.grid(row=0, column=1)
Entry2.config(command= lambda arg=counter2:EntryCB(arg))
Entry2.configure(borderwidth=3, relief='sunken', bg='pink')
Entry3 = Button(frame1, textvariable=counter3)
Entry3.grid(row=0, column=2)
Entry3.config(command= lambda arg=counter3:EntryCB(arg))
Entry3.configure(borderwidth=3, relief='sunken', bg='orange')
#setup callback and display control buttons to increment or decrement the IntVar
Inc = Button(frame1,text='Inc')
Inc.grid(row=1, column=0)
Inc.configure(command= lambda arg=cntr:increment(arg))
Dec = Button(frame1,text='Dec')
Dec.grid(row=1, column=1)
Dec.configure(command= lambda arg=cntr:decrement(arg))
root.mainloop()