【问题标题】:Tkinter buttons bellow grid to close and save to fileTkinter 按钮在网格下方关闭并保存到文件
【发布时间】: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


    【解决方案1】:

    首先,您的 Class 的缩进级别已关闭。这些方法需要缩进另一个级别,否则每个方法都会收到 TypeError。

    其次,对于buttonQbuttonS,请确保您引用了该类的实例,即:

    buttonQ = Button(self.root, text = "Quit", command = endProgam)
    buttonS = Button(self.root, text = "Save", command = saveToFile)
    

    应该是:

    buttonQ = Button(self.root, text = "Quit", command = self.endProgam)
    buttonS = Button(self.root, text = "Save", command = self.saveToFile)
    

    (注意使用self

    就实际放置按钮而言,我建议创建一个额外的框架来单独管理布局。您可以像小部件一样创建和放置它们,这有助于使管理布局更加简单。

    例如:

    class App():
        def __init__(self, root):
            self.root = root
            self.TopFrame = Frame(root) # Create a top frame to place the original grid
            self.BottomFrame = Frame(root) # Create a frame for the additional buttons
            self.TopFrame.grid(row=0) # Place the Frame itself
            self.BottomFrame.grid(row=6) # Place the new Frame directly below the first
    
            # Changed to an instance variable to reference in Function method
            buttonQ = Button(self.BottomFrame, text="Quit", command=self.endProgam)
            buttonS = Button(self.BottomFrame, text="Save", command=self.saveToFile)
            buttonS.grid(row=0, column=0, padx=10)
            buttonQ.grid(row=0, column=1, padx=10)
    
        def Function(self):
            self.grid = []
            for i in range(5):
                row = []
                for j in range(5):
                    row.append(Button(self.TopFrame,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)
    

    注意新的TopFrameBottomFrame。网格按钮现在位于 TopFrame 上,而 BottomFrame 包含两个新的按钮小部件。 您会发现将单独的布局对象放置在其自己的框架中将使管理更复杂的布局变得更加简单。

    【讨论】:

    • 别忘了this
    • 好点。我没有测试按钮命令本身。我更新了我的答案以传递函数而不是值。谢谢!
    • 您的命令有误。在代码示例中,您有 command=self.endProgram(),但它必须是 command=self.endProgram)
    • 别忘了致电App.Function()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多