简体   繁体   English

Tkinter网格管理器(TypeError)

[英]Tkinter Grid Manager (TypeError)

I'm trying to put a set of 16 pictures into a tkinter frame. 我正在尝试将一组16张图片放入tkinter帧中。 I've created a dictionary that maps the image address name (stored in imgbutton) as a key to the dictionary imagedict the value of which is a string containing its position in the tkinter grid (example a button in column 0 and row=1 would have the name backgroundpic5.jpg and the value "01"). 我创建了一个字典,该字典将图像地址名称(存储在imgbutton中)映射为字典imagedict的键,其值是一个字符串,其中包含其在tkinter网格中的位置(例如,第0列和row = 1中的按钮会具有名称backgroundpic5.jpg和值“ 01”)。

Each row contains 4 pictures and there are 4 rows in total. 每行包含4张图片,总共有4行。 The PhotoImage equivalent of imgbutton is buttonphoto (created using PIL). 等效于imgbutton的PhotoImage是buttonphoto(使用PIL创建)。 However, when i try running this, it tells me the grid_configure command accepts on 2 arguments while i have given 3 (Type Error) 但是,当我尝试运行此命令时,它告诉我grid_configure命令接受2个参数,而我给出了3个(类型错误)

Pos=imagedict[imgbutton]
GridColumn=Pos[0]; GridRow=Pos[1]
Button= tk.Button(root, image=buttonphoto)
Button.grid(GridColumn, GridRow)
Button.pack()

The error statement, 错误陈述,

Traceback (most recent call last):
  File "C:\Users\USER\Desktop\ComputerProject\Tester2Imagebg.py", line 44, in <module>
  Button.grid(GridColumn, GridRow)
  TypeError: grid_configure() takes at most 2 arguments (3 given)

Thanks :) 谢谢 :)

Listen to the error. 听错误。 It knows what it's talking about ... well anyway it tells you something. 它知道它在说什么...无论如何它可以告诉您一些信息。 Look up the documentation for Tkinter.Button.grid and it will tell. 查找有关Tkinter.Button.grid的文档,它会告诉您。 You need to do Button.grid(column=GridColumn, row=GridRow) 您需要执行Button.grid(column=GridColumn, row=GridRow)

You see, the error is right. 您看,错误是对的。 You gave one too many arguments to Button.grid() . 您给Button.grid()提供了太多参数。 Using Button.grid(...) is really just a shortcut for tk.Button.grid(Button, ...) , so all together you gave three arguments, but .grid() is expecting a button instance, cnf , and some keyword arguments. 使用Button.grid(...)实际上只是tk.Button.grid(Button, ...)的快捷方式,因此一起给了三个参数,但是.grid()期望有一个按钮实例cnf和一些关键字参数。 You provide the button instance by using Button.grid(...) instead of tk.Button(...) , but you gave two other positional arguments. 您可以通过使用Button.grid(...)而不是tk.Button(...)提供按钮实例,但是还提供了另外两个位置参数。 You need to provide the column and row numbers as keyword arguments. 您需要提供列号和行号作为关键字参数。

使用grid()布局管理器选项的正确语法:

 Button.grid(column = GridColumn, row = GridRow)

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

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