Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Tkinter Python在网格视图中创建列表_Python_List_Tkinter_Grid - Fatal编程技术网

使用Tkinter Python在网格视图中创建列表

使用Tkinter Python在网格视图中创建列表,python,list,tkinter,grid,Python,List,Tkinter,Grid,我正在尝试使用python中的Tkinter创建产品的网格视图列表,您能告诉我如何做到这一点吗 我尝试使用网格(行、列)来实现这一点,但这也不起作用 我想添加这样的产品列表,我已经尝试过了,但没有成功 要对所需的项目进行网格化,需要使用grid()方法 from tkinter import Tk, Label root = Tk() colors = ['yellow', 'red', 'blue', 'green'] # first create the labels or any o

我正在尝试使用python中的Tkinter创建产品的网格视图列表,您能告诉我如何做到这一点吗

我尝试使用
网格(行、列)
来实现这一点,但这也不起作用

我想添加这样的产品列表,我已经尝试过了,但没有成功


要对所需的项目进行网格化,需要使用
grid()
方法

from tkinter import Tk, Label

root = Tk()

colors = ['yellow', 'red', 'blue', 'green']

# first create the labels or any other widgets you need and grid them
for i in range(len(colors)):
    Label(root, bg=colors[i], width=50).grid(row=i, column=0, sticky='NSWE')
    
    # then configure the weight of each widget so as for them to resize dynamically
    root.grid_rowconfigure(i, weight=1) # each row has the same weight
root.grid_columnconfigure(0, weight=1) # column 0 where the labels are also has weight = 1



root.mainloop()


看看这个问题,这个问题能回答你的问题吗?您不应该将'root.grid\u columnconfigure(0,weight=1)`放入
for
循环中。它只需要运行一次。@TheLizzard你是对的