Python 7x4网格显示1个按钮28次,而不是显示28个不同的按钮。为什么?

Python 7x4网格显示1个按钮28次,而不是显示28个不同的按钮。为什么?,python,tkinter-layout,Python,Tkinter Layout,我想在7x4自动调整大小的网格中显示下面的28个按钮。现在,equal按钮显示了28次。我需要在7x4网格中显示所有28个不同的按钮。我用row_index和col_index变量配置了所有28个不同的按钮,但它仍然不起作用。如果你能帮我指引正确的方向,我将不胜感激 类计算器(ttk.Frame) def初始化(自身): root=Tk() root.title(“TK的计算器”) 根几何(“700x500”) 计算器() …您正在将所有控件放置在行=行索引,列=列索引。运行时,行索引为7,列

我想在7x4自动调整大小的网格中显示下面的28个按钮。现在,equal按钮显示了28次。我需要在7x4网格中显示所有28个不同的按钮。我用row_index和col_index变量配置了所有28个不同的按钮,但它仍然不起作用。如果你能帮我指引正确的方向,我将不胜感激

类计算器(ttk.Frame) def初始化(自身): root=Tk() root.title(“TK的计算器”) 根几何(“700x500”)

计算器()

您正在将所有控件放置在
行=行索引,列=列索引。运行时,
行索引
为7,
列索引
为4。您需要硬编码所有这些按钮的位置,如
行索引=0,列索引=3
。那就行了。我现在正在看:


对于格式问题,我深表歉意。您的
按钮
行缩进得太远了一步。它们是for loop列的一部分,将重复4次TIM,当我将按钮行缩进一行时,根本没有打印任何内容。你说得对,很多时候,正确的缩进会产生巨大的影响。非常感谢。我要试一试!嗨,Tim,我将for循环中的变量从row_index/column index更改为row和column,然后按照您的建议对值进行硬编码。它工作得很好!非常感谢。我将按钮网格正确地显示在固定的窗口大小上,但我真正想做的是,使按钮网格在窗口最大化时自动调整到更大的大小。你帮了我!!!
enter code here
    Grid.rowconfigure(root, 0, weight=1)
    Grid.columnconfigure(root, 0, weight=1)

    root.resizable(True, True)
    
    frame1 = Frame(root, bg="#80c1ff", bd=5)
    frame1.grid(row=0, column=0, sticky="nsew")

    for row_index in range(7):
        Grid.rowconfigure(frame1, row_index, weight=1)
        
    for col_index in range(4):
        Grid.columnconfigure(frame1, col_index, weight=1)
        
        buttonMC = Button(frame1, text="MC", height=3, width=5, command=MC)
        buttonMC.grid(row=row_index, column=col_index, sticky="nsew")
        buttonMC.configure(width=7, height=3, font=("Verdana", 10, "bold"), fg="Navy")

        MRButton = Button(frame1, text="MR", height=3, width=5, command=MR)
        MRButton.grid(row=row_index, column=col_index, sticky="nsew")
        MRButton.configure(width=7, height=3, font=("Verdana", 10, "bold"), fg="Navy")

        MPlusButton = Button(frame1, text="M+", height=3, width=5, command=MPlus)
        MPlusButton.grid(row=row_index, column=col_index, sticky="nsew")
        MPlusButton.configure(width=7, height=3, font=("Verdana", 10, "bold"), fg="Navy")

        percentButton = Button(frame1, text="%", height=3, width=5, command=percent)
        percentButton.grid(row=row_index, column=col_index, sticky="nsew")
        percentButton.configure(width=7, height=3, font=("Verdana", 10, "bold"), fg="Navy")

        buttonModulo = Button(frame1, text="Mod", height=3, width=5, command=modulo)
        buttonModulo.grid(row=row_index, column=col_index, sticky="nsew")
        buttonModulo.configure(width=7, height=3, font=("Verdana", 10, "bold"), fg="Navy")

        buttonSqRoot = Button(frame1, text="√(x)", height=3, width=5, command=squareRootOfx)
        buttonSqRoot.grid(row=row_index, column=col_index, sticky="nsew")
        buttonSqRoot.configure(width=7, height=3, font=("Verdana", 10, "bold"), fg="Navy")

        button2ndPower = Button(frame1, text="x²", height=3, width=5, command=powerOf2)
        button2ndPower.grid(row=row_index, column=col_index, sticky="nsew")
        button2ndPower.configure(width=7, height=3, font=("Verdana", 10, "bold"), fg="Navy")

        buttonOneOverX = Button(frame1, text="1/x", height=3, width=5, command=oneOverX)
        buttonOneOverX.grid(row=row_index, column=col_index, sticky="nsew")
        buttonOneOverX.configure(width=7, height=3, font=("Verdana", 10, "bold"), fg="Navy")

        buttonCE = Button(frame1, text="CE", height=3, width=5, command=clearEntry)
        buttonCE.grid(row=row_index, column=col_index, sticky="nsew")
        buttonCE.configure(width=7, height=3, font=("Verdana", 10, "bold"), fg="Navy")

        buttonClear = Button(frame1, text="C", height=3, width=5, command=clear)
        buttonClear.grid(row=row_index, column=col_index, sticky="nsew")
        buttonClear.configure(width=7, height=3, font=("Verdana", 10, "bold"), fg="Navy")

        buttonDelete = Button(frame1, text="del", height=3, width=5, command=delete)
        buttonDelete.grid(row=row_index, column=col_index, sticky="nsew")
        buttonDelete.configure(width=7, height=3, font=("Verdana", 10, "bold"), fg="Navy")

        buttonDivide = Button(frame1, text="/", height=3, width=5, command=divide)
        buttonDivide.grid(row=row_index, column=col_index, sticky="nsew")
        buttonDivide.configure(width=7, height=3, font=("Verdana", 10, "bold"), fg="Navy")

        button7 = Button(frame1, text="7", height=3, width=5, command=lambda: buttonClick(7))
        button7.grid(row=row_index, column=col_index, sticky="nsew")
        button7.configure(width=7, height=3, font=("Verdana", 10, "bold"), fg="Navy")

        button8 = Button(frame1, text="8", height=3, width=5, command=lambda: buttonClick(8))
        button8.grid(row=row_index, column=col_index, sticky="nsew")
        button8.configure(width=7, height=3, font=("Verdana", 10, "bold"), fg="Navy")

        button9 = Button(frame1, text="9", height=3, width=5, command=lambda: buttonClick(9))
        button9.grid(row=row_index, column=col_index, sticky="nsew")
        button9.configure(width=7, height=3, font=("Verdana", 10, "bold"), fg="Navy")
        

        buttonMultiply = Button(frame1, text="x", height=3, width=5, command=multiply)
        buttonMultiply.grid(row=row_index, column=col_index, sticky="nsew")
        buttonMultiply.configure(width=7, height=3, font=("Verdana", 10, "bold"), fg="Navy")

        button4 = Button(frame1, text="4", height=3, width=5, command=lambda: buttonClick(4))
        button4.grid(row=row_index, column=col_index, sticky="nsew")
        button4.configure(width=7, height=3, font=("Verdana", 10, "bold"), fg="Navy")

        button5 = Button(frame1, text="5", height=3, width=5, command=lambda: buttonClick(5))
        button5.grid(row=row_index, column=col_index, sticky="nsew")
        button5.configure(width=7, height=3, font=("Verdana", 10, "bold"), fg="Navy")

        button6 = Button(frame1, text="6", height=3, width=5, command=lambda: buttonClick(6))
        button6.grid(row=row_index, column=col_index, sticky="nsew")
        button6.configure(width=7, height=3, font=("Verdana", 10, "bold"), fg="Navy")

        buttonMinus = Button(frame1, text="-", height=3, width=5, command=subtract)
        buttonMinus.grid(row=row_index, column=col_index, sticky="nsew")
        buttonMinus.configure(width=7, height=3, font=("Verdana", 10, "bold"), fg="Navy")

        button1 = Button(frame1, text="1", height=3, width=5, command=lambda: buttonClick(1))
        button1.grid(row=row_index, column=col_index, sticky="nsew")
        button1.configure(width=7, height=3, font=("Verdana", 10, "bold"), fg="Navy")

        button2 = Button(frame1, text="2", height=3, width=5, command=lambda: buttonClick(2))
        button2.grid(row=row_index, column=col_index, sticky="nsew")
        button2.configure(width=7, height=3, font=("Verdana", 10, "bold"), fg="Navy")

        button3 = Button(frame1, text="3", height=3, width=5, command=lambda: buttonClick(3))
        button3.grid(row=row_index, column=col_index, sticky="nsew")
        button3.configure(width=7, height=3, font=("Verdana", 10, "bold"), fg="Navy")

        buttonAdd = Button(frame1, text="+", height=3, width=5, command=add)
        buttonAdd.grid(row=row_index, column=col_index, sticky="nsew")
        buttonAdd.configure(width=7, height=3, font=("Verdana", 10, "bold"), fg="Navy")
        
        buttonPlusMinus = Button(frame1, text="+/-", height=3, width=5, command=plusMinus)
        buttonPlusMinus.grid(row=row_index, column=col_index, sticky="nsew")
        buttonPlusMinus.configure(width=7, height=3, font=("Verdana", 10, "bold"), fg="Navy")

        buttonZero = Button(frame1, text="0", height=3, width=5, command=lambda: buttonClick(0))
        buttonZero.grid(row=row_index, column=col_index, sticky="nsew")
        buttonZero.configure(width=7, height=3, font=("Verdana", 10, "bold"), fg="Navy")

        buttonDecimal = Button(frame1, text=".", height=3, width=5, command=decimal)
        buttonDecimal.grid(row=row_index, column=col_index, sticky="nsew")
        buttonDecimal.configure(width=7, height=3, font=("Verdana", 10, "bold"), fg="Navy")

        buttonEquals = Button(frame1, text="=", height=3, width=5, command=equals)
        buttonEquals.grid(row=row_index, column=col_index, sticky="nsew")
        buttonEquals.configure(width=7, height=3, font=("Verdana", 10, "bold"), fg="Navy")

    frame2 = Frame(root, bg="lightgrey", bd=5)
    frame2.grid(row=0, column=1, sticky="nsew")

    historyButton = Button(frame2, text="Clear History", fg="Navy", bg="#80c1ff", command=clearhistory)
    historyButton.configure(font=("Verdana", 12, "bold"), fg="Navy")
    historyButton.place(x=5, y=40)

    memoryButton = Button(frame2, text="Memory", bg="#80c1ff", fg="navy", command=memory)
    memoryButton.configure(font=("Verdana", 12, "bold"), fg="Navy")
    memoryButton.place(x=135, y=40)

    self.result = Entry(frame2, width=24, borderwidth=3)
    self.result.configure(font=("Verdana", 14))
    self.result.grid(row=0, column=0, pady=5)

    root.mainloop()