在Python tkinter中如何获取用户输入值而不调用输入标签名称或使用get()方法。

huangapple go评论65阅读模式
英文:

How to get the user input values without calling the entry label name or using the get() methid in python tkinter

问题

I can help you with the translation. Here's the translated code:

from tkinter import *

matrix = [[]]

# 验证用户输入是否为数字
def number():
    input = int(E1.get())
    # 如果用户输入是数字,则创建一个输入矩阵
    try:
        int(input)
        for x in range(input):
            for y in range(input):
                matrixInput = Entry(root, bg="white")
                matrixInput.grid(row=10+x, column=1+y, padx=5, pady=5)

        B2 = Button(root, text="提交", font=40, command=storeMatrix)
        B2.grid(row=20, column=1)

    except ValueError:
        L2.config(text="您没有输入数字")


def storeMatrix():
    temp = []
    for x in range(input):
        for y in range(input):
            temp.append(matrixInput.get())


root = Tk()
L1 = Label(root, text="有多少人", font=40)
L1.grid(row=0, column=0)

E1 = Entry(root, bg="white")
E1.grid(row=0, column=1)

L2 = Label(root, text='')
L2.grid(row=3, column=1)

B1 = Button(root, text="提交", font=40, command=number)
B1.grid(row=2, column=1)

root.mainloop()

I've translated the code, replacing the text within the code with Chinese text while leaving the code structure intact.

英文:

I'm pretty new to using python tkinter and I have created a matrix of entries based on user input. Like for example if the user types in 3, the program will output a 3x3 matrix. The issue I'm having is that I'm not able to figure out to extract the user data from the matrix of entries. This is my code:

from tkinter import *

matrix = [[]]

#Validate that the user entered a number
def number():
    input = int(E1.get())
    #Creates a matrix of entries if the user entered a number
    try:
        int(input)
        for x in range(input):
            for y in range(input):
                matrixInput = Entry(root, bg = "white")
                matrixInput.grid(row=10+x, column=1+y, padx=5, pady=5)
                
    
        B2 = Button(root, text="Submit", font = 40, command=storeMatrix)
        B2.grid(row=20, column=1)
            
    except ValueError:
        L2.config(text = "You did not enter a number")


def storeMatrix():
    temp = []
    for x in range(input):
        for y in range(input):
            temp.append(matrixInput.get())



root = Tk()
L1 = Label(root, text="How many people", font = 40)
L1.grid(row=0, column=0)

E1 = Entry(root, bg = "white")
E1.grid(row=0, column=1)


L2 = Label(root, text = '')
L2.grid(row=3, column=1)


B1 = Button(root, text="Submit", font = 40, command=number)
B1.grid(row=2, column=1)

root.mainloop()

在Python tkinter中如何获取用户输入值而不调用输入标签名称或使用get()方法。

Any help on how to fix this issue would be amazing. Thank you.

答案1

得分: 2

你需要将所有的输入字段保存在一个全局变量中。matrixInput 是局部于 number() 函数的,并且仅包含最后创建的输入。

你可以使用 matrix 列表来实现这个目标。

matrix = []

def number():
    input = int(E1.get())
    # 如果用户输入了一个数字,则创建一个输入矩阵
    try:
        int(input)
        for x in range(input):
            matrixRow = []
            for y in range(input):
                matrixInput = Entry(root, bg="white")
                matrixInput.grid(row=10+x, column=1+y, padx=5, pady=5)
                matrixRow.append(matrixInput)
            matrix.append(matrixRow)
    
        B2 = Button(root, text="Submit", font=40, command=storeMatrix)
        B2.grid(row=20, column=1)
            
    except ValueError:
        L2.config(text="您没有输入一个数字")

def storeMatrix():
    temp = []
    for x in range(input):
        for y in range(input):
            temp.append(matrix[x][y].get())
英文:

You need to save all the input fields in a global variable. matrixInput is local to the number() function, and also only contains the last input that was created.

You can use the matrix list for this.

matrix = []

def number():
    input = int(E1.get())
    #Creates a matrix of entries if the user entered a number
    try:
        int(input)
        for x in range(input):
            matrixRow = []
            for y in range(input):
                matrixInput = Entry(root, bg = "white")
                matrixInput.grid(row=10+x, column=1+y, padx=5, pady=5)
                matrixRow.append(matrixInput)
            matrix.append(matrixRow)
    
        B2 = Button(root, text="Submit", font = 40, command=storeMatrix)
        B2.grid(row=20, column=1)
            
    except ValueError:
        L2.config(text = "You did not enter a number")

def storeMatrix():
    temp = []
    for x in range(input):
        for y in range(input):
            temp.append(matrix[x][y].get())

huangapple
  • 本文由 发表于 2023年6月22日 00:18:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76525313.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定