【问题标题】:Tkinter Dynamic Dictionary for Matrix- Cant use .get method on entry windowsTkinter Dynamic Dictionary for Matrix-不能在入口窗口上使用 .get 方法
【发布时间】:2018-07-18 21:35:51
【问题描述】:

我一直在开发一个程序,它可以生成用户定义大小的矩阵,然后可以对矩阵执行操作。该程序分为几个部分。

首先,用户输入行和列,当提交时,将打开一个带有输入框矩阵的 tkinter 窗口。

每个输入框都是动态entries 字典中的一个值。列表gridrowsgridcols 用于为.grid 方法提供用于排列输入框的坐标。

我遇到的问题是在将用户的值输入矩阵后分配一个命令来获取用户输入。我尝试创建一个函数storevals,它将所有矩阵条目附加到matrixinput 列表中。

当我运行这个程序时,我得到矩阵输入窗口,在矩阵的每个框中输入值后,我从第 50 行得到相同的错误消息:AttributeError:"NoneType" object has no attribute "get"。输入框似乎没有类型,在.grid排列窗口后似乎丢失了。

有没有办法将这些窗口中的用户条目分配给一个列表?任何帮助将不胜感激。

谢谢!

from matplotlib import pyplot as plt
from tkinter import *

#Take user input for matrix size and store in matrixrows and matrixcols variables as integers.
master = Tk()
master.title("Matrix Size")
Label(master, text = "Rows").grid(row=0)
Label(master, text = "Columns").grid(row=1)


def storevals():
    matrixrows.append(int(rows.get()))
    matrixcols.append(int(cols.get()))
    master.quit()

matrixrows = []
matrixcols = []


rows = Entry(master)
cols = Entry(master)

rows.grid(row = 0, column = 1)
cols.grid(row =1, column = 1)

Button(master, text= "Enter", command = storevals).grid(row=3, column =0, sticky=W, pady=4)
mainloop()

norows = matrixrows[0]
nocols = matrixcols[0]
matrixlist =[]

for i in range(0,norows):
    matrixlist.append([])

for i in range(0,norows):
    for j in range(0,nocols):
        matrixlist[i].append(nocols*0)



#Generate a matrix entry window with entries correpsonding to the matrix

master2 = Tk()
master2.title("Enter Matrix Values")
matrixinput=[]

def storevals():
    for key in entries:
        matrixinput.append(entries[key].get())
        print(matrixinput)


Button(master2, text= "Submit Matrix", command = storevals).grid(row=norows+1, column =round(nocols/2), sticky=W, pady=4)

gridrows=[]
gridcols=[]


#creates two lists, gridcols and gridrows which are used to align the entry boxes in the tkinter window

for i in range(0,norows):
    for j in range(0,nocols):
        gridrows.append(i)

for i in range(0,norows):
    for j in range(0,nocols):
        gridcols.append(j)


entries ={}
for x in range(0,nocols*norows):
           entries["Entrybox{0}".format(x)]=Entry(master2).grid(row=gridrows[x], column=gridcols[x])

print(entries)

mainloop()

【问题讨论】:

    标签: python loops matrix tkinter


    【解决方案1】:

    您可能还没有查看在 mainloop() 之前打印的条目内容

    entries ={}
    for x in range(0,nocols*norows):
               entries["Entrybox{0}".format(x)]=Entry(master2).grid(row=gridrows[x], column=gridcols[x])
    
    print(entries)
    

    grid()pack()place() 都返回 None

    如果你想保留小部件,你需要将它分配给一个变量,然后调用grid()

    entries ={}
    for x in range(0,nocols*norows):
        widget = Entry(master2)
        widget.grid(row=gridrows[x], column=gridcols[x])
        entries["Entrybox{0}".format(x)] = widget
    
    print(entries)
    

    【讨论】:

      猜你喜欢
      • 2019-08-08
      • 2016-06-10
      • 2013-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-27
      相关资源
      最近更新 更多