矩阵不能使用的动态字典。在输入窗口上获取方法

2024-10-01 19:29:31 发布

您现在位置:Python中文网/ 问答频道 /正文

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

首先,用户输入行和列,当提交时,将打开一个带有输入框矩阵的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()

Tags: 用户inmasterforrange矩阵gridrow
1条回答
网友
1楼 · 发布于 2024-10-01 19:29:31

您可能还没有查看在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)

相关问题 更多 >

    热门问题