【发布时间】:2017-10-17 01:42:56
【问题描述】:
我正在尝试创建一个使用 Tkinter 更改颜色的按钮网格。
from tkinter import *
class App():
def __init__(self, root):
self.root = root
buttonQ = Button(self.root, text = "Quit", command = endProgam())
buttonS = Button(self.root, text = "Save", command = saveToFile())
def Function(self):
self.grid = []
for i in range(5):
row = []
for j in range(5):
row.append(Button(self.root,width=6,height=3,command=lambda i=i, j=j: self.Click1(i, j),background='gray'))
row[-1].grid(row=i,column=j)
self.grid.append(row)
def Click1(self, i, j):
orig_color = self.grid[i][j].cget('bg')
#print(orig_color)
if orig_color=="red":
self.grid[i][j]["bg"]="gray"
else:
self.grid[i][j]["bg"]="red"
#self.grid[i][j]["bg"]="red"
#self.grid[i][j].configure(background="blue")
def endProgam(self):
# top.quit()
top.destroy()
def saveToFile(self):
# save matrix to file
top.destroy()
root = Tk()
app = App(root)
app.Function()
root.mainloop()
我的问题是我无法在网格下方添加 2 个按钮,一个用于退出,一个用于根据按钮颜色(0-灰色和 1-红色作为矩阵)保存到文件值,然后退出。
File "--", line 37, in <module>
app = App(root)
File "--", line 6, in __init__
buttonQ = Button(self.root, text = "Quit", command = endProgam())
TypeError: endProgam() missing 1 required positional argument: 'self'
这是我第一次使用 Tkinter 在 Python 中编码,所以请温柔:)
【问题讨论】:
标签: python python-3.x button tkinter grid