473,748 Members | 6,664 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

tkinter, annoying grid-problem

so my little calculator works perfectly now. just having some trouble
with the layout.
this whole tkinter-thing seems to be more tricky than it should be.
how can i make the 4 column of buttons have the same distance and
size between them as the other 3 columns?
and how can i make the top entry end where the 2nd row entry
ends(meaning the top entry will be longer)?

why are the 4th row split from the others? hard to fix the problems
when u dont even understand why things happen. seems so llogical a lot
of it. i change something then something unexpected happens.

from __future__ import division
import Tkinter
from Tkinter import *

mygui = Tkinter.Tk()

mygui.title("Ca lculator")

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

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

c = Entry(mygui)
c.grid(row=2, column=3, columnspan=4, sticky=W)

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(INSER T, END)
#e.delete(ANCHO R,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, sticky=W)
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, sticky=W)
b = Button(mygui, text="C",comman d=Erase, width=2, height=1)
b.grid(row=8, column=3, sticky=W)
b = Button(mygui, text="c",comman d=Backspace, width=2, height=1)
b.grid(row=8, column=4, sticky=W)
b = Button(mygui, text="=",comman d=Calc, width=18, height=1)
b.grid(row=9, column=1, columnspan=4, sticky=W)

mygui.mainloop( )
Jun 27 '08 #1
2 3530
On Friday 11 April 2008 18:41,
sk*******@yahoo .se wrote:
so my little calculator works perfectly
now. just having some trouble with the
layout.
this whole tkinter-thing seems to be more
tricky than it should be. how can i make
the 4 column of buttons have the same
distance and size between them as the
other 3 columns? and how can i make the
top entry end where the 2nd row entry
ends(meaning the top entry will be
longer)?

why are the 4th row split from the others?
hard to fix the problems when u dont even
understand why things happen. seems so
llogical a lot of it. i change something
then something unexpected happens.
Look this file over.

I built two additional frames.
You can control the looks of the progect
easier with the additional frames.

The calculator DOES NOT WORK. You will have
to have to play with it to get the results
that you want, which you can do.

jim-on-linux
http://www.inqvista.com
############### ############### ########
class Calc :
def __init__ (self) :
self.mygui = Tk()
self.mygui.titl e("Calculator ")
self.MkButtons( )

def MkButtons(self) :
mygui = self.mygui
dataFra = Frame(mygui)
dataFra.grid(ro w = 0, column = 0)
l = Label(dataFra, text="Answer: ")
l.grid(row=0, column=0, sticky=EW)
self.e = Entry(dataFra)
self.e.grid(row =1, column=0,
sticky=EW)
c = Entry(dataFra)
c.grid(row=2, column=0, sticky=EW)

x = 1
y = 4

butFra = Frame(mygui)
butFra.grid(row =1, column=0)
for n in '123+456-789*0()/.':
b = Button(butFra, text= n,
command =
lambda :self.Disp(n),
width=2, height=1)
b.grid(row=y, column=x, sticky=W)
x=x+1
if x==5:
x=1
y=y+1
b = Button(butFra, text="^",
command=self.Di sp, width=2,
height=1)
b.grid(row=8, column=2, sticky=W)
b = Button(butFra,
text="C",comman d=self.Erase, width=2,
height=1)
b.grid(row=8, column=3, sticky=W)
b = Button(butFra,
text="c",comman d=self.Backspac e, width=2,
height=1)
b.grid(row=8, column=4, sticky=W)
b = Button(butFra,
text="=",comman d=self.Calc, width=18,
height=1)
b.grid(row=9, column=1, columnspan=4,
sticky=EW)

def Disp(self, n):
print n, '## n cal 68\n'
n = ord(n)
self.e.insert( 0,str( n))

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

def Backspace(self) :
a=len(e.get())
e.delete(a-1,END)
#e.delete(INSER T, END)
#e.delete(ANCHO R,END)

if __name__ == '__main__' :
Calc()
mainloop()

############### ############### #####
############### ############### #####
from __future__ import division
import Tkinter
from Tkinter import *

mygui = Tkinter.Tk()

mygui.title("Ca lculator")

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

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

c = Entry(mygui)
c.grid(row=2, column=3, columnspan=4,
sticky=W)

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(INSER T, END)
#e.delete(ANCHO R,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, sticky=W)
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, sticky=W)
b = Button(mygui, text="C",comman d=Erase,
width=2, height=1) b.grid(row=8, column=3,
sticky=W) b = Button(mygui,
text="c",comman d=Backspace, width=2,
height=1) b.grid(row=8, column=4,
sticky=W) b = Button(mygui,
text="=",comman d=Calc, width=18, height=1)
b.grid(row=9, column=1, columnspan=4,
sticky=W)

mygui.mainloop( )
Jun 27 '08 #2
sk*******@yahoo .se wrote:
so my little calculator works perfectly now. just having some trouble
with the layout.
this whole tkinter-thing seems to be more tricky than it should be.
how can i make the 4 column of buttons have the same distance and
size between them as the other 3 columns?
and how can i make the top entry end where the 2nd row entry
ends(meaning the top entry will be longer)?

why are the 4th row split from the others? hard to fix the problems
when u dont even understand why things happen. seems so llogical a lot
of it. i change something then something unexpected happens.
The best answer I can give (being a Tk expert but not yet a tkinter
expert) is to start with a piece of graph paper. Draw the GUI out and
you'll probably see what the problems are. For one, the second entry
(for the answer) spans 4 columns and begins at column 3, so it ends up
in column 6. This ends up affecting the whole layout because nothing
else goes to column six.

You'll probably find it much easier going to break down your GUI into
sections. One for the calculator buttons and one for everything else.
Create a frame for the buttons and it becomes trivial to layout out all
the buttons in a 4x6 grid, unaffected by things outside that grid. Then,
create your other widgets and grid the whole frame of buttons as a
single unit inside the outermost frame. I quite often use grid for
interior groupings, then use pack on the outter-most frame to manager
the various groups.

Using the grid layout manager is trivial if you do a little thinking and
planning up front. If you don't, you can spend all day chasing down why
you end up with an extra blank row or column, unusually sized rows and
columns, etc. Again, get a piece of graph paper and draw it out -- that
helps immensely when you're first coming up to speed using grid.
Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
5326
by: Patrick L. Nolan | last post by:
Our Tkinter application has a big ScrolledText widget which is a log of everything that happens. In order to prevent people from making changes, we have it in DISABLED mode except when the program wants to write a new entry. This works OK, except that sometimes we want to copy out of piece of the contents and paste it in another window. When it's DISABLED, it appears that we can't even select a portion of the text. Is this an...
2
17658
by: anx | last post by:
I've got a grid-managed frame, containing a column of Labels, and a corresponding column of Entry widgets. I'd like to be able to display dozens, or even hundreds of rows, and use a vertical scrollbar to scroll through them in the frame. So far i can get a scrollbar to display, but it won't scroll anything. So that's definately wrong. And the columns just chop off at the bottom of the frame, but resizing the window won't make the frame...
3
3611
by: dwelch91 | last post by:
I'm trying unsuccessfully to do something in Tk that I though would be easy. After Googling this all day, I think I need some help. I am admittedly very novice with Tk (I started with it yesterday), so I am sure I am overlooking something simple. The basic idea is that my application will consist of a series of modal dialogs, that are chained together in "wizard" fashion. There will be some processing in between dialogs, but for the most...
1
8343
by: Stan Cook | last post by:
A newbie to Tkinter here. . . . . . I'm trying to set the focus on an Entry textbox with focus_set. I am using the grid manager. I created the same interface before using the pack() method and the focus_set worked, but now it says "AttributeError: 'NoneType' object has no attribute 'focus_set'" Below is the section of the code:
5
2034
by: crystalattice | last post by:
I'm creating a shelve interface using Tkinter. I have a button that allows the user to modify an existing entry; when the button is clicked, a new TopLevel window appears with Entry boxes holding the selected entry. Below the Entry boxes are two buttons, one that saves the changes to the database and the other is simply a Cancel button. Under Linux, both buttons appear correctly. However, in OS X the Cancel button is invisible unless...
6
2513
by: Adam | last post by:
Hey, I'm pretty new to programming. Been trying to learn using Python. The code I'm struggling with is for my GUI. I'm am having trouble getting this to display the way I want with the grid manager. Can anybody tell me what I am doing wrong? I hope you can tell what look I'm trying to achieve from the code as its hard to explain.
3
30212
by: ElBeardo | last post by:
Hello, I´m new to Python.. so this is a newbee question. I´d like to put the value enterd in the entryfield in a variable. I´m trying to build a calculator with python and TKinter, coding it in just python works good. But making it with TK is a bit hard. This is my unfinished code, i have tryed get() in many ways,.but I cant make it work. I´m only need to get knowledge about using get() now,.. I now there is other thing undone in this...
1
5663
kaarthikeyapreyan
by: kaarthikeyapreyan | last post by:
Beginners Game- Tic-Tac-Toe My first attempt after learning Tkinter from Tkinter import * import tkFont class myApp: """ Defining The Geometry of the GUI And the variables Used In the The methods
1
3027
by: skanemupp | last post by:
in this program when using the "c"-button it deletes the last token entered. i want to delete the token after the mousecursor. lets say the string is: 12*(81**.5+12) and i put the cursor between the * and * because i want times .5 not root. now if i press "c" it deletes ")" which is not what i want. ive tried coming up with a function that does what i want but neither of the ways as shown below works. i want to do something like...
0
1530
by: Kevin McKinley | last post by:
Below i've put the code for a program that i wrote. I need help on lines 384-403. If you run this program you will notice on the first tab when have it produce an answer the $ is surrounded with {$}. How can i get rid of that? from Tkinter import * class MyApp: def __init__(self, parent): self.myparent = parent
0
8987
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9366
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9316
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9241
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4597
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4867
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3303
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2211
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.