Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #75576

Re: Tkinter grid autosize help

From Terry Reedy <tjreedy@udel.edu>
Subject Re: Tkinter grid autosize help
Date 2014-08-02 23:22 -0400
References <60f53f24-7ce7-48e5-9fed-7d9ac8841b7f@googlegroups.com> <bfd8ce20-b31f-4b32-a286-5efece366b73@googlegroups.com> <lrk62h$nb0$1@ger.gmane.org>
Newsgroups comp.lang.python
Message-ID <mailman.12568.1407036172.18130.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Tkinter grid autosize help Nicholas Cannon <nicholascannon1@gmail.com> - 2014-08-02 07:38 -0700
  Re: Tkinter grid autosize help MRAB <python@mrabarnett.plus.com> - 2014-08-02 16:33 +0100
  Re: Tkinter grid autosize help Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-08-02 17:14 +0100
  Re: Tkinter grid autosize help Nicholas Cannon <nicholascannon1@gmail.com> - 2014-08-02 15:53 -0700
    Re: Tkinter grid autosize help Terry Reedy <tjreedy@udel.edu> - 2014-08-02 22:16 -0400
    Re: Tkinter grid autosize help Rick Johnson <rantingrickjohnson@gmail.com> - 2014-08-02 19:24 -0700
    Re: Tkinter grid autosize help Terry Reedy <tjreedy@udel.edu> - 2014-08-02 23:22 -0400
  Re: Tkinter grid autosize help Nicholas Cannon <nicholascannon1@gmail.com> - 2014-08-02 19:36 -0700

csiph-web