2017-05-14 1 views
0

3x3 매트릭스의 엔트리와 그 엔트리가 서로 상호 작용하기를 원하는 엔트리를 만들고 싶습니다.엔트리로 채워지는 행렬의 인덱스 얻기

float 값을 입력하고 항목의 Enter 키를 누를 때마다 (첫 번째와 두 번째 열에 있음) 원하는 세 번째 열에 행의 첫 번째와 두 번째 열의 합계를 표시합니다. 수정되었습니다. 이처럼

:

| 0.0| 2.5| 2.5| 
| 2.4| 1.5| 3.9| 
| 1.1| 2.2| 3.3| 

이 내 코드입니다 : 내가 Enter 키를 누를 때 나는 수정 된 항목의 인덱스를 얻을 수있는 방법

from Tkinter import * 

global i,j,m 

def compute(master): 
    suma=0.0 
    for x in range (0,1): 
     suma+=float(m[i][x].get()) 
    if not i==0: 
     suma+=float(m[i-1][2].get()) 
    print suma 
    m[i][2].insert(str(suma)) 


root = Tk() 

alto =3 
ancho=3 


m = [[0 for x in range(alto)] for y in range(ancho)] 



for i in range (0, alto): 
    for j in range (0, ancho): 
     m[i][j]= Entry(root) 
     m[i][j].grid(row = i,column = j) 
     m[i][j].insert(0,"0.0") 
     if (j==0 or j==1): 
      m[i][j].bind('<Return>', compute) 
     else: 
      m[i][j].config(state='readonly') 

root.mainloop() 

?

가변 행렬과 모든 항목의 현재 값을 비교할 수 있지만 다음에는 많은 값을 가진 데이터베이스를 사용할 것이므로 많은 자원을 사용할 수 있다는 것을 알고 있습니다.

+0

'스마 = 0,0'해야한다'스마 = 0.0' ... – jasonharper

+0

: 여기

이 솔루션

브라이언 오클리, 요르단 글리슨 모든 열의 가치를 계산하는 것 같으므로 항목의 색인이 필요한 이유가 있습니까? 바운드 함수에는 변경된 실제 위젯에 대한 참조가있는 객체가 제공됩니다. –

+0

비교 기능에서 참조가 "마스터"입니까? 어떤 위젯이 "마스터"로 변경되었는지 어떻게 알 수 있습니까? –

답변

0

많은 작은 오류가 있으므로 코드를 디버깅하는 방법에 대해 배우는 것처럼 보입니다. 다음은 몇 가지 간단한 디버깅을 통해 잘못되었을 수있는 몇 가지 사항에 대한 주석이있는 코드입니다. 여기에 게시하기 전에이 물건을 찾아 내려고 노력하면서 시간을 보내십시오.

from Tkinter import * 

global i, j, m 

# "You should rename master because that is a very misleading name. The 
# canonical name is event, though some prefer evt." – Bryan Oakley 
def compute(master): 
    suma = 0.0 
    # This for loop will only iterate once. You need range(0, 2): for it to go 
    # through the code twice 
    for x in range(0, 1): 
     suma += float(m[i][x].get()) 

    # I can't figure out the purpoes of this 
    if not i == 0: 
     suma += float(m[i - 1][2].get()) 
    print suma 

    # You've done it properly in the for loop, but not here. insert() needs an 
    # index as its first parameter 
    m[i][2].insert(str(suma)) 


root = Tk() 

alto = 3 
ancho = 3 
m = [[0 for x in range(alto)] for y in range(ancho)] 

# Because i is a global, and the below for loop, i is only ever going to 
# be equal to 2, which means your compute code is only going to compute 
# the 3rd row. 
for i in range(0, alto): 
    for j in range(0, ancho): 
     m[i][j] = Entry(root) 
     m[i][j].grid(row=i, column=j) 
     m[i][j].insert(0, "0.0") 
     if (j == 0 or j == 1): 
      m[i][j].bind('<Return>', compute) 
     else: 
      m[i][j].config(state='readonly') 

root.mainloop() 

여기에 고정 된 버전의 계산 기능이 있습니다. 시간을내어 내가 겪은 변화를 이해하고 이해하십시오.

def compute(master): 
    # Loop through each row in the grid 
    for i in range(0, alto): 
     suma = 0.0 
     for x in range(0, 2): 
      # I've left in the prints I've used to debug as exmaples for you 
      print "i", i 
      print "get", m[i][x].get() 
      suma += float(m[i][x].get()) 
     print "suma", suma, "\n" 
     # You cannot insert into a readonly entry, so set it to normal first 
     m[i][2].config(state='normal') 
     # Inserting every time will add the string to the beginning of the 
     # entry, so that it builds up on every run. Instead delte the conetents 
     # of the entry first, and then insert. 
     m[i][2].delete(0, END) 
     m[i][2].insert(0, str(suma)) 
     # Set the entry back to readonly 
     m[i][2].config(state='readonly') 
+1

'master'는 매우 오도하는 이름이기 때문에 이름을 변경해야합니다. 정식 명칭은'event'이지만 일부는'evt'를 선호합니다. –

0

많은 도움을 주셔서 감사합니다. 저는 파이썬과 tkinter에서 새로운 사람입니다. 나는 그 문제를 해결했다. 특별 감사에 :

from Tkinter import * 

global m 

def indexes(event): 
    for i in range(0, 3): 
     for j in range(0, 3): 
      if event.widget == m[i][j]: 
       return i,j 

def compute(event): 
    i,j = indexes(event) 

    suma=0.0 
    for x in range (0,2): 
     suma+=float(m[i][x].get()) 

    m[i][2].config(state='normal') 
    m[i][2].delete(0, END) 
    m[i][2].insert(0,str(suma)) 
    m[i][2].config(state='readonly') 



root = Tk() 

alto =3 
ancho=3 

sv = [[0 for x in range(alto)] for y in range(ancho)] 
m = [[0 for x in range(alto)] for y in range(ancho)] 

for i in range (0, alto): 
    for j in range (0, ancho): 
     #sv [i] [j] = StringVar() 
     #sv[i][j] = "0.0" 
     m[i][j]= Entry(root)#, textvariable=sv [i] [j]) 
     m[i][j].grid(row = i,column = j) 
     m[i][j].insert(0,"0.0") 
     if (j==0 or j==1): 
      m[i][j].bind('<Return>', compute) 
     else: 
      m[i][j].config(state='readonly') 

root.mainloop() 
관련 문제