简体   繁体   English

如何在2D阵列中配置“ tkinter”对象?

[英]How to configure 'tkinter' objects within a 2D array?

I've used 'for' loops to create tables made from 'Label' objects. 我使用了“ for”循环来创建由“ Label”对象制成的表。 Apart from the 'divider', each loop appends to it's own two-dimensional array(each column has thirteen 'Labels'); 除了“除法器”,每个循环都附加到自己的二维数组(每列有十三个“标签”); I wanted to give every object an index position within their respective tables, so that I can then reference those points and alter the contents of the 'Label'. 我想给每个对象各自表中的索引位置,这样我就可以引用这些点并更改“标签”的内容。

#--------------------------------
    entry_grid_exp=[]
    entry_grid_req=[]
#--------------------------------
    exp_list = Frame(program, bg="#dcdcdc", height=500, width=600)
    exp_list.grid(row=3, column=1)
#--------------------------------
    for i in range(1, 16):
        entry_column=[]
        for x in range(0, 13):
            entry_column.append(Label(exp_list, width=15,bg="#FFFFFF",relief=SUNKEN))
            entry_column[x].grid(row = x+6, column=i, padx=0, pady=0)
        entry_grid_exp.append(entry_column)
    Request_list = Label(exp_list, width=10, bg="#dcdcdc", fg="#000000", text="Requested list.")
    Request_list.grid(row = 20, column=1)
    for i in range(1,16):
        box3 = Label(exp_list, width=10, bg="#dcdcdc", fg="#dcdcdc", text="divider")
    box3.grid(row = 21, column=i)
    for i in range(1, 16):
        entry_column=[]
        for x in range(0, 6):
            entry_column.append(Label(exp_list, width=15,bg="#FFFFFF",relief=SUNKEN))
            entry_column[x].grid(row = x+21, column=i, padx=0, pady=0)
         entry_grid_req.append(entry_column)
#--------------------------------

A submit button calls the function 'stock_append' and passes on the 'entry_grid_exp' and 'entry_grid_req' arguments. 提交按钮调用函数“ stock_append”并传递“ entry_grid_exp”和“ entry_grid_req”参数。 I read a post which said that placing the list into a 'for' loop and attributing every item within the list to 'Label' will allow me to call the 'configure' function, but it doesn't seem to work. 我读了一篇帖子,说将列表放入“ for”循环并将列表中的每个项目归于“标签”将使我能够调用“配置”功能,但似乎不起作用。 Additionally this loop (if it worked) would configure every item within 'entry_grid_exp' rather than one specific position. 另外,此循环(如果可行)将配置“ entry_grid_exp”中的每个项目,而不是一个特定位置。

def stock_append(entry_grid_exp, entry_grid_req):
for Label in entry_grid_exp:
    Label.configure(text="test") 

I'm trying to complete this project and am completely lost, and absolutely appreciate any help because I have no idea what I'm doing. 我正在尝试完成这个项目,但完全迷路了,非常感谢任何帮助,因为我不知道自己在做什么。

UPDATED EXAMPLE 更新示例

from tkinter import *
root = Tk()
Med_font = ("Helvatica", 11)
Small_font = ("Helvatica", 9)
main = Frame(root, bg="#dcdcdc", height=600, width=600)
main.grid()
s_name = StringVar()
s_category = StringVar()
s_description = StringVar()
s_quantity = StringVar()
s_risk = StringVar()
s_ID = StringVar()


def stock_append(entry_grid_exp, entry_grid_req, program):
    for item in entry_grid_exp:
        Label.configure(program, text="test")


def stock_define(entry_grid_exp, entry_grid_req, program):
    stock_frame = Frame(main, bg="#dcdcdc", height=100, width=1000)
    stock_frame.grid()

    name = Entry(stock_frame, width=15, textvariable=s_name)
    name.grid(row=1, column=1, padx=10)

    category = Entry(stock_frame, width=15, textvariable=s_category)
    category.grid(row=1, column=2, padx=10)

    description = Entry(stock_frame, width=15, textvariable=s_description)
    description.grid(row=1, column=3, padx=10)

    quantity = Entry(stock_frame, width=15, textvariable=s_quantity)
    quantity.grid(row=1, column=4, padx=10)

    risk = Entry(stock_frame, width=15, textvariable=s_risk)
    risk.grid(row=1, column=5, padx=10)

    stock_id = Entry(stock_frame, width=15, textvariable=s_ID)
    stock_id.grid(row=1, column=6, padx=10)
    submit = Button(stock_frame, width=15, text="submit", command=lambda: stock_append(entry_grid_exp, entry_grid_req,
                                                                                  program))
    submit.grid(row=1, column=7, padx=10)


def stock_lists():
    program = Frame(main, bg="#dcdcdc", height=600, width=600)
    program.grid()

    title = Label(program, text="MAIN", font=Med_font, bg="#dcdcdc")
    title.grid(row=2, column=1)

    entry_grid_exp = []
    entry_grid_req = []

    exp_list = Frame(program, bg="#dcdcdc", height=500, width=600)
    exp_list.grid(row=3, column=1)

    stock_add = Button(exp_list, bg="#bebebe", width=10, text="ADDSTCK", command=lambda: stock_define(entry_grid_exp,
                                                                                                  entry_grid_req,
                                                                                                  program))
    stock_add.grid(row=4, column=15)

    experiment_list = Label(exp_list, width=10, bg="#dcdcdc", fg="#000000", text="Experiment list.")
    experiment_list.grid(row=4, column=1)

    for i in range(1, 16):
        entry_column = []
        for x in range(0, 13):
            entry_column.append(Label(exp_list, width=15, bg="#FFFFFF", relief=SUNKEN))
            entry_column[x].grid(row=x+6, column=i, padx=0, pady=0)
        entry_grid_exp.append(entry_column)

    for i in range(1, 16):
        divider = Label(exp_list, width=10, bg="#dcdcdc", fg="#dcdcdc", text="divider")
        divider.grid(row=21, column=i)

    request_list = Label(exp_list, width=10, bg="#dcdcdc", fg="#000000", text="Requested list.")
    request_list.grid(row=20, column=1)

    for i in range(1, 16):
        entry_column = []
        for x in range(0, 6):
            entry_column.append(Label(exp_list, width=15, bg="#FFFFFF", relief=SUNKEN))
            entry_column[x].grid(row=x+21, column=i, padx=0, pady=0)
        entry_grid_req.append(entry_column)

stock_lists()
mainloop()

Your updated example is better, but it's certainly not minimal. 您更新的示例更好,但是肯定不是最少。 We don't need that large grid of Labels to illustrate your problem. 我们不需要那么大的标签网格来说明您的问题。 (I have an old monitor so my desktop is only 1024 x 768 pixels, which makes it painful to view your GUI). (我有一台旧显示器,所以我的桌面只有1024 x 768像素,这使得查看GUI变得很痛苦)。 Also, it's still not totally clear what you want the program to do. 另外,还不清楚您要让程序执行什么操作。 But anyway... 但无论如何...

Your entry_grid_exp , entry_grid_req are both lists of lists, with the inner lists being columns of Labels, so to access those Labels you need to use an appropriate loop structure. 您的entry_grid_expentry_grid_req都是列表列表,内部列表是Label的列,因此要访问这些Label,您需要使用适当的循环结构。

Here's a version of stock_append that gives each Label a text based on its row and column number. 这是stock_append的一个版本,该版本根据行号和列号为每个Label提供文本。

def stock_append(entry_grid_exp, entry_grid_req, program):
    x = 0
    for col in entry_grid_exp:
        y = 0
        for item in col:
            item.configure(text="exp{}_{}".format(y, x))
            y += 1
        x += 1

    x = 0
    for col in entry_grid_req:
        y = 0
        for item in col:
            item.configure(text="req{}_{}".format(y, x))
            y += 1
        x += 1

Here's a more compact version that uses the built-in enumerate function to produce the row and column numbers: 这是一个更紧凑的版本,它使用内置的enumerate函数生成行号和列号:

def stock_append(entry_grid_exp, entry_grid_req, program):
    for x, col in enumerate(entry_grid_exp):
        for y, item in enumerate(col):
            item.configure(text="exp{}_{}".format(y, x))

    for x, col in enumerate(entry_grid_req):
        for y, item in enumerate(col):
            item.configure(text="req{}_{}".format(y, x))

The entry_grid_exp and entry_grid_req lists are local to the stock_lists function. entry_grid_expentry_grid_req列表是stock_lists函数的本地列表。 If you want them to exist after stock_lists finishes running you need to return them to the code that calls stock_lists . 如果希望它们在stock_lists完成运行之后存在, stock_lists需要将它们返回到调用stock_lists的代码中。

BTW, the name of the font is "Helvetica", not "Helvatica". 顺便说一句,字体的名称是“ Helvetica”,而不是“ Helvatica”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM