【问题标题】:controlling multiple entry buttons with the same 2 control buttons使用相同的 2 个控制按钮控制多个输入按钮
【发布时间】:2017-10-13 19:27:49
【问题描述】:

我相信这段代码很接近 - 我定义了三个按钮,并使用回调来存储与所选按钮关联的 IntVar() 变量。我创建了一个存储适当 Intvar 的临时变量 (cntr)。这似乎是基于我在回调中的打印语句。但是当我选择其中一个按钮然后点击在代码末尾配置的 inc 或 dec 按钮时,唯一改变的“入口”按钮是在代码的主要部分中设置的那个。谁能告诉我我错过了什么

#!/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()

【问题讨论】:

    标签: python-3.x tkinter


    【解决方案1】:

    好的-多玩一点-这是一个答案-但是使用我发布的原始代码必须有更优雅的东西-我只是不明白为什么原始代码不起作用。提前感谢您查看它

    from tkinter import *
    from tkinter import PhotoImage
    
    def increment (event):
        global cntr
        print('in inc', cntr)
        cntr.set("{0:0>4}".format(cntr.get() + 1))
    
    def decrement (event):
        global cntr
        print('in dec', cntr)
        cntr.set("{0:0>4}".format(cntr.get() - 1))
    
    def EntryCB(arg):
        global cntr, counter1, counter2, counter3
        if arg == 1:
            cntr=counter1
        elif arg == 2:
            cntr=counter2
        elif arg == 3:
            cntr=counter3
        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=1
    
    print('in main', cntr)
    
    Entry1 = Button(frame1, textvariable=counter1)
    Entry1.grid(row=0, column=0)
    Entry1.config(command= lambda arg=1: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=2: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=3:EntryCB(arg))
    Entry3.configure(borderwidth=3, relief='sunken', bg='orange')
    
    Inc = Button(frame1,text='Inc')
    Inc.bind("<Button-1>",increment)
    Inc.grid(row=1, column=0)
    #Inc.configure(command= lambda arg=cntr:increment(arg))
    Dec = Button(frame1,text='Dec')
    Dec.bind("<Button-1>",decrement)
    Dec.grid(row=1, column=1)
    #Dec.configure(command= lambda arg=cntr:decrement(arg))
    
    root.mainloop()
    

    【讨论】:

      猜你喜欢
      • 2016-04-19
      • 1970-01-01
      • 1970-01-01
      • 2017-08-18
      • 1970-01-01
      • 2012-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多