2017-10-13 12 views
0

私はこのコードが近いと思います - 選択したボタンに関連付けられたIntVar()変数を格納するコールバックで3つのボタンが定義されています。私は適切なIntvarを格納する一時変数(cntr)を作成しました。それは私がコールバックで持っている印刷ステートメントに基づいて動作しているようです。しかし、それらのボタンの1つを選択してコードの最後に設定されたincまたはdecボタンを押すと、変更される唯一の「入力」ボタンがコードのメインセクションに設定されたボタンになります。もう少しそれで遊んで - - ここに1つの答えである - しかし、私は掲載元のコードを使用して、よりエレガントなものが存在しなければならない - 誰もが私は複数の入力ボタンを同じ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() 

答えて

0

[OK]ををしないのですが私を見ることができます私は理解していません元のコードが機能しない理由見ていただきありがとうございます。

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() 
関連する問題