On 8/2/2014 10:16 PM, Terry Reedy wrote:
On 8/2/2014 6:53 PM, Nicholas Cannon wrote:
The only way i can make the buttons look neat and then when i keep
pressing one the label gets larger and then half the buttons
move out of the screen
With my code below, I tried entering a 20 digit number and the button
boxes separate horizontally. This is not exactly what you describe, but
it does mess up the initially neat display.
is there a way i can stop the grid from expanding?
One thing I might do, besides using an entry box, it to grid the buttons
in a separate frame. I wrote the code below, with the header_rows
variable, with that in mind.
This sort of repetitious code is crying for a loop. For one thing, if
you want to change the buttons, there should only be one Button call to
modify. Since I am still learning to write tkinter myself, I wrote the
following, which I suspect does what you wanted and a bit more.
from tkinter import *
main = Tk()
main.title('Calculator')
main.geometry('300x350')
#main.resizable() # does nothing
app = Frame(main)
app.grid()
total = IntVar()
total.set(0)
entry = StringVar()
entry.set('')
Label(app, text='Total').grid(row=0, column=0)
Label(app, textvariable=total).grid(row=0, column=1, columnspan=3)
Label(app, text='Entry').grid(row=1, column=0)
Label(app, textvariable=entry).grid(row=1, column=1, columnspan=3)
def append(digit):
entry.set(entry.get() + digit)
def add():
total.set(total.get() + int(entry.get()))
entry.set('')
def sub():
total.set(total.get() - int(entry.get()))
entry.set('')
header_rows = 2
for num, r, c in (
('7', 0, 0), ('8', 0, 1), ('9', 0, 2),
('4', 1, 0), ('5', 1, 1), ('6', 1, 2),
('1', 2, 0), ('2', 2, 1), ('3', 2, 2),
('0', 3, 0), ('+', 3, 1), ('-', 3, 2),):
cmd = {'+':add, '-':sub}.get(num, lambda num=num: append(num))
b = Button(app, text=num, command=cmd, width=5)
b.grid(row=header_rows+r, column=c)
main.mainloop()
With regard to your next message: If you do not understand the function
definitions above, including the lambda expression, and the loop, you
should definitely look more at the tutorial.
--
Terry Jan Reedy
--
https://mail.python.org/mailman/listinfo/python-list