【问题标题】:How to input Matrix in tkinter?如何在 tkinter 中输入矩阵?
【发布时间】:2020-05-11 12:42:11
【问题描述】:

我希望用户在 tkinter GUI 中输入矩阵。当用户按下“提交”时,它应该显示输入的矩阵。我知道如何以正常方式输入矩阵,但不知道如何在“tkinter”中执行。 告诉我如何在 tkinter 中定义矩阵,在 Entry 中写什么来代替“textvariable”。

这是我的代码:

from tkinter import *
window = Tk()
window.title("Matrix")
window.geometry("650x500+120+120")
window.configure(bg='bisque2')
window.resizable(False, False)
m1 = StringVar()

Label(window, text="Enter matrix :", font=('arial', 10, 'bold'), 
bg="bisque2").place(x=20, y=20)

x2 = 0
y2 = 0
rows, cols = (3,3)
for i in range(rows):
  for j in range(cols):

        entry = Entry(window, textvariable =  ,width=3)
        entry.place(x=60 + x2, y=50 + y2)
        x2 += 30

  y2 += 30
  x2 = 0
button= Button(window,text="Submit", bg='bisque3', width=15)
button.place(x=160,y=140)
window.mainloop()

【问题讨论】:

    标签: python python-3.x matrix tkinter


    【解决方案1】:

    您可以为此使用StringVar()s 数组。

    from tkinter import Tk, Label, StringVar, Button, Entry
    window = Tk()
    window.title("Matrix")
    window.geometry("650x500+120+120")
    window.configure(bg='bisque2')
    window.resizable(False, False)
    
    # empty arrays for your Entrys and StringVars
    text_var = []
    entries = []
    
    # callback function to get your StringVars
    def get_mat():
        matrix = []
        for i in range(rows):
            matrix.append([])
            for j in range(cols):
                matrix[i].append(text_var[i][j].get())
    
        print(matrix)
    
    Label(window, text="Enter matrix :", font=('arial', 10, 'bold'), 
          bg="bisque2").place(x=20, y=20)
    
    x2 = 0
    y2 = 0
    rows, cols = (3,3)
    for i in range(rows):
        # append an empty list to your two arrays
        # so you can append to those later
        text_var.append([])
        entries.append([])
        for j in range(cols):
            # append your StringVar and Entry
            text_var[i].append(StringVar())
            entries[i].append(Entry(window, textvariable=text_var[i][j],width=3))
            entries[i][j].place(x=60 + x2, y=50 + y2)
            x2 += 30
    
        y2 += 30
        x2 = 0
    button= Button(window,text="Submit", bg='bisque3', width=15, command=get_mat)
    button.place(x=160,y=140)
    
    window.mainloop()
    

    【讨论】:

      猜你喜欢
      • 2014-12-20
      • 2015-02-18
      • 1970-01-01
      • 1970-01-01
      • 2014-05-09
      • 1970-01-01
      • 1970-01-01
      • 2018-04-19
      • 1970-01-01
      相关资源
      最近更新 更多