Prevent GUI layout from changing?

Discussion in 'Python' started by skanemupp@yahoo.se, Apr 6, 2008.

  1. Guest

    when i added the results-LABEL the buttons have changed place.
    meh why cant i just put buttons and stuff on a specific coordinate and
    have them staying there?




    #! /usr/bin/env python
    from Tkinter import *
    import tkMessageBox

    class GUIFramework(Frame):
    """This is the GUI"""

    def __init__(self, master=None):
    """Initialize yourself"""

    self.expr = ""

    """Initialise the base class"""
    Frame.__init__(self,master)

    """Set the Window Title"""
    self.master.title("Calculator")

    """Display the main window"
    with a little bit of padding"""
    self.grid(padx=10,pady=10)
    self.CreateWidgets()



    def CreateWidgets(self):

    ## """Create the Entry, set it to be a bit wider"""
    ## self.enText = Entry(self)
    ## self.enText.grid(row=0, column=0, columnspan=3)

    """Create the Button, set the text and the
    command that will be called when the button is clicked"""
    self.btnDisplay = Button(self, text="calculate!",
    command=self.Calculate)
    self.btnDisplay.grid(row=0, column=31)

    """Create the Button, set the text and the
    command that will be called when the button is clicked"""
    self.lbText = Label(self, text="Results: ")
    self.lbText.grid(row=1, column=0)


    self.btnDisplay = Button(self,text='1',command=lambda
    n="1":self.Display(n),width=1,height=1)
    self.btnDisplay.grid(row=3, column=0, padx=5, pady=5)

    self.btnDisplay = Button(self,text='2',command=lambda
    n="2":self.Display(n),width=1,height=1)
    self.btnDisplay.grid(row=3, column=1, padx=5, pady=5)

    self.btnDisplay = Button(self,text='3',command=lambda
    n="3":self.Display(n),width=1,height=1)
    self.btnDisplay.grid(row=3, column=2, padx=5, pady=5)

    self.btnDisplay = Button(self,text='+',command=lambda
    n="+":self.Display(n),width=1,height=1)
    self.btnDisplay.grid(row=3, column=3, padx=5, pady=5)

    self.btnDisplay = Button(self,text='4',command=lambda
    n="4":self.Display(n),width=1,height=1)
    self.btnDisplay.grid(row=4, column=0, padx=5, pady=5)

    self.btnDisplay = Button(self,text='5',command=lambda
    n="5":self.Display(n),width=1,height=1)
    self.btnDisplay.grid(row=4, column=1, padx=5, pady=5)

    self.btnDisplay = Button(self,text='6',command=lambda
    n="6":self.Display(n),width=1,height=1)
    self.btnDisplay.grid(row=4, column=2, padx=5, pady=5)

    self.btnDisplay = Button(self,text='-',command=lambda
    n="-":self.Display(n),width=1,height=1)
    self.btnDisplay.grid(row=4, column=3, padx=5, pady=5)

    self.btnDisplay = Button(self,text='7',command=lambda
    n="7":self.Display(n),width=1,height=1)
    self.btnDisplay.grid(row=5, column=0, padx=5, pady=5)

    self.btnDisplay = Button(self,text='8',command=lambda
    n="8":self.Display(n),width=1,height=1)
    self.btnDisplay.grid(row=5, column=1, padx=5, pady=5)

    self.btnDisplay = Button(self,text='9',command=lambda
    n="9":self.Display(n),width=1,height=1)
    self.btnDisplay.grid(row=5, column=2, padx=5, pady=5)

    self.btnDisplay = Button(self,text='*',command=lambda
    n="*":self.Display(n),width=1,height=1)
    self.btnDisplay.grid(row=5, column=3, padx=5, pady=5)

    self.btnDisplay = Button(self,text='0',command=lambda
    n="0":self.Display(n),width=1,height=1)
    self.btnDisplay.grid(row=6, column=0, padx=5, pady=5)

    self.btnDisplay = Button(self,text='C',command=self.Clean)
    self.btnDisplay.grid(row=6, column=1, padx=5, pady=5)

    self.btnDisplay = Button(self,text='r',command=lambda
    n="r":self.Display(n),width=1,height=1)
    self.btnDisplay.grid(row=6, column=2, padx=5, pady=5)

    self.btnDisplay = Button(self,text='/',command=lambda
    n="/":self.Display(n),width=1,height=1)
    self.btnDisplay.grid(row=6, column=3, padx=5, pady=5)




    def Display(self, number):
    self.expr = self.expr + number
    self.lbText = Label(self, text=self.expr)
    self.lbText.grid(row=0, column=0)

    def Calculate(self):
    self.expr = str(eval(self.expr))#try catch tex 3+6+
    self.lbText = Label(self, text=self.expr)
    self.lbText.grid(row=1, column=1)
    self.expr = ""

    def Clean(self):
    self.expr = ""
    #self.update()


    if __name__ == "__main__":
    guiFrame = GUIFramework()
    guiFrame.mainloop()
    , Apr 6, 2008
    #1
    1. Advertising

  2. En Sun, 06 Apr 2008 15:12:55 -0300, <> escribió:

    I can't help with your sizing problem, I don't know grids. But don't do
    this:

    > def Display(self, number):
    > self.expr = self.expr + number
    > self.lbText = Label(self, text=self.expr)
    > self.lbText.grid(row=0, column=0)
    >
    > def Calculate(self):
    > self.expr = str(eval(self.expr))#try catch tex 3+6+
    > self.lbText = Label(self, text=self.expr)
    > self.lbText.grid(row=1, column=1)
    > self.expr = ""


    You're creating a *new* Label object for each keystroke (they stack on the
    same place and only the newest is visible, I presume).
    Instead, you should change the text inside the existing widget:

    self.lbText.config(text=self.expr)

    (there is no need to reposition the widget)
    Label is described here http://effbot.org/tkinterbook/label.htm
    and you may want to learn to use Tkinter variables:
    http://effbot.org/tkinterbook/variable.htm

    --
    Gabriel Genellina
    Gabriel Genellina, Apr 6, 2008
    #2
    1. Advertising

  3. Guest

    On 6 Apr, 22:15, "Gabriel Genellina" <> wrote:
    > En Sun, 06 Apr 2008 15:12:55 -0300, <> escribió:
    >
    > I can't help with your sizing problem, I don't know grids. But don't do
    > this:
    >
    > > def Display(self, number):
    > > self.expr = self.expr + number
    > > self.lbText = Label(self, text=self.expr)
    > > self.lbText.grid(row=0, column=0)

    >
    > > def Calculate(self):
    > > self.expr = str(eval(self.expr))#try catch tex 3+6+
    > > self.lbText = Label(self, text=self.expr)
    > > self.lbText.grid(row=1, column=1)
    > > self.expr = ""

    >
    > You're creating a *new* Label object for each keystroke (they stack on the
    > same place and only the newest is visible, I presume).
    > Instead, you should change the text inside the existing widget:
    >
    > self.lbText.config(text=self.expr)
    >
    > (there is no need to reposition the widget)
    > Label is described herehttp://effbot.org/tkinterbook/label.htm
    > and you may want to learn to use Tkinter variables: http://effbot.org/tkinterbook/variable.htm
    >
    > --
    > Gabriel Genellina



    the problem is that when i start writing long numbers the window grows
    bigger which is really annoying. is that ebcause of what u said?

    as of now i can see exactly the expression i typed and the answer it
    is just annoing that it doesnt stay the same size.
    , Apr 7, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rick Spiewak
    Replies:
    3
    Views:
    3,147
    Rick Spiewak
    Aug 26, 2003
  2. RobertH
    Replies:
    1
    Views:
    718
    Steve C. Orr [MVP, MCSD]
    Nov 4, 2003
  3. NWx
    Replies:
    4
    Views:
    2,959
    Kevin Spencer
    Feb 19, 2004
  4. Eric
    Replies:
    4
    Views:
    709
    clintonG
    Dec 24, 2004
  5. Replies:
    1
    Views:
    571
    John Timney \(MVP\)
    Jun 19, 2006
Loading...

Share This Page