Tkinter, resize window, keep widgets relative placements?

S

skanemupp

the code is down below. when i click maximize window it looks terrible
since the widgets are not keeping their relative size.
i guess i could use pack or grid to do that instead of place?
but i tried with pack and grid before and had trouble making it
looking good.

is it possible to have the minimized window just being placed in the
middle without the distance between the buttons and entrys being
enlonge>?


from __future__ import division
import Tkinter
from Tkinter import *

mygui = Tkinter.Tk()

mygui.title("Calculator")

l = Label(mygui, text="Answer: ")
l.place(relx=0.15, rely=0.2, anchor=CENTER)

e = Entry(mygui)
e.place(relx=0.48, rely=0.1, anchor=CENTER, width=173)

c = Entry(mygui)
c.place(relx=0.6, rely=0.2, anchor=CENTER)

def Disp(nstr):
e.insert(INSERT, nstr)

def Calc():
expr=e.get()
c.delete(0, END)
try:
c.insert(END, eval(expr))
except:
c.insert(END, "Not computable")

def Erase():
e.delete(0,END)
c.delete(0, END)

def Backspace():
a=len(e.get())
e.delete(a-1,END)
#e.delete(INSERT, END)
#e.delete(ANCHOR,END)


x = 0.1
y = 0.4
for char in '123+456-789*0()/.':
b = Button(mygui, text=char, command=lambda n=char:Disp(n),
width=2, height=1)
b.place(relx=x, rely=y, anchor=CENTER)
x=x+0.1
if x==0.5:
x=0.1
y=y+0.1

b = Button(mygui, text="^", command=lambda n="**":Disp(n), width=2,
height=1)
b.place(relx=0.2, rely=0.8, anchor=CENTER)
b = Button(mygui, text="C",command=Erase, width=2, height=1)
b.place(relx=0.3, rely=0.8, anchor=CENTER)
b = Button(mygui, text="c",command=Backspace, width=2, height=1)
b.place(relx=0.4, rely=0.8, anchor=CENTER)
b = Button(mygui, text="=",command=Calc, width=12, height=1)
b.place(relx=0.25, rely=0.9, anchor=CENTER)

mygui.mainloop()
 
S

skanemupp

here i didi it with pack() but when i try to use the answerwidget it
gtes all f***** up. any suggestions?


from __future__ import division
import Tkinter
from Tkinter import *

mygui = Tkinter.Tk()

mygui.title("Calculator")

##l = Label(mygui, text="Answer: ")
##l.grid(row=2, column=1, columnspan=2)

e = Entry(mygui)
e.grid(row=1, column=1, columnspan=4)

c = Entry(mygui)
c.grid(row=2, column=1, columnspan=4)

def Disp(nstr):
e.insert(INSERT, nstr)

def Calc():
expr=e.get()
c.delete(0, END)
try:
c.insert(END, eval(expr))
except:
c.insert(END, "Not computable")

def Erase():
e.delete(0,END)
c.delete(0, END)

def Backspace():
a=len(e.get())
e.delete(a-1,END)
#e.delete(INSERT, END)
#e.delete(ANCHOR,END)


x = 1
y = 4
for char in '123+456-789*0()/.':
b = Button(mygui, text=char, command=lambda n=char:Disp(n),
width=2, height=1)
b.grid(row=y, column=x)
x=x+1
if x==5:
x=1
y=y+1

b = Button(mygui, text="^", command=lambda n="**":Disp(n), width=2,
height=1)
b.grid(row=8, column=2)
b = Button(mygui, text="C",command=Erase, width=2, height=1)
b.grid(row=8, column=3)
b = Button(mygui, text="c",command=Backspace, width=2, height=1)
b.grid(row=8, column=4)
b = Button(mygui, text="=",command=Calc, width=18, height=1)
b.grid(row=9, column=1, columnspan=8)

mygui.mainloop()
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top