Reputation: 101
I would like to display the 28 buttons below in a 7x4 self-resizing grid. As it stands now, the equal button is being displayed 28 times. I need all 28 different buttons to display in the 7x4 grid. I have all 28 different buttons configured with the row_index and col_index variables, but it's still not working. If you could help steer me in the right direction, I'd greatly appreciate it.
...
enter code here
class Calculator(ttk.Frame) def init(self): root = Tk() root.title("TK's Calculator") root.geometry("700x500")
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()
Calculator() ...
Upvotes: 0
Views: 55
Reputation: 54649
You are placing all of your controls at row=row_index, column=col_index
. By the time that runs, row_index
is 7 and col_index
is 4. You need to hard-code the locations for all of those buttons, as in row_index=0, col_index=3
. Then it will work. I'm looking at it now:
Upvotes: 1