我想制作一个3x3的条目矩阵和那些我希望它们互相交互的条目。
我希望每次引入浮点值然后按条目内的Enter键(仅在第一列和第二列)我希望第三列显示行中第一列和第二列的总和被修改了。
像这样:
| 0.0| 2.5| 2.5|
| 2.4| 1.5| 3.9|
| 1.1| 2.2| 3.3|
这是我的代码:
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()
按Enter键时,如何修改条目索引?
我知道这可能是比较变量矩阵和所有条目的当前值,但可能会使用大量资源,因为在此旁边我将使用具有大量值的数据库。
答案 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 :(得分:0)
非常感谢你的帮助。我是python和tkinter的新手。我解决了这个问题。特别感谢:
Bryan Oakley和Jordan Gleeson
以下是解决方案:
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()