Need help with Tkinter window formatting using Python

0 votes

Hi guys. I've been practicing my Tkinter skills by trying to make a simple calculator, but I'm having issues properly lining up the input forms and the buttons

Here's my current code

from Tkinter import *

def calculate():
    try:
        num1 = float(enter1.get())
        num2 = float(enter2.get())
        result = num1 * num2
        label3.config(text=str(result))
    except ValueError:
        label3.config(text='Enter numeric values!')
def calculate2():
    try:
        num1 = float(enter1.get())
        num2 = float(enter2.get())
        result = num1 / num2
        label3.config(text=str(result))
    except ValueError:
        label3.config(text='Enter numeric values!')
def calculate3():
    try:
        num1 = float(enter1.get())
        num2 = float(enter2.get())
        result = num1 + num2
        label3.config(text=str(result))
    except ValueError:
        label3.config(text='Enter numeric values!')
def calculate4():
    try:
        num1 = float(enter1.get())
        num2 = float(enter2.get())
        result = num1 - num2
        label3.config(text=str(result))
    except ValueError:
        label3.config(text='Enter numeric values!')

root = Tk()



label1 = Label(root, text='First Number:')
label1.grid(row=0, column=1)
enter1 = Entry(root, bg='white')
enter1.grid(row=1, column=1)


label2 = Label(root, text='Second Number:')
label2.grid(row=2, column=1)
enter2 = Entry(root, bg='white')
enter2.grid(row=3, column=1)


btn1 = Button(root, text='X', command=calculate)
btn1.grid(row=4, column=1)
btn2 = Button(root, text='/', command=calculate2)
btn2.grid(row=5, column=1)
btn3 = Button(root, text='+', command=calculate3)
btn3.grid(row=5, column=2)
btn4 = Button(root, text='-', command=calculate4)
btn4.grid(row=4, column=2)

label3 = Label(root)
label3.grid(row=6, column=1)


enter1.focus()

enter1.bind('<Return>', func=lambda e:enter2.focus_set())

root.mainloop()


This is what my output looks like
What the Widget looks like now
I would like to line up all the operator buttons under the input field. How can i do that.

Sep 7, 2018 in Python by charlie_brown
• 7,720 points
914 views

1 answer to this question.

0 votes

Tkininter comes with the columnspan argument to span the labels and the entries two columns. As a side note, remember that the column index is also zero-based:

label1.grid(row=0, column=0, columnspan=2)
enter1.grid(row=1, column=0, columnspan=2)
label2.grid(row=2, column=0, columnspan=2)
enter2.grid(row=3, column=0, columnspan=2)

btn1.grid(row=4, column=0)
btn2.grid(row=5, column=0)
btn3.grid(row=5, column=1)
btn4.grid(row=4, column=1)

label3.grid(row=6, column=0, columnspan=2)

answered Sep 7, 2018 by aryya
• 7,460 points

Related Questions In Python

0 votes
1 answer
+1 vote
1 answer

Need some help with Python memory leaks

As far as best practices, keep an ...READ MORE

answered Nov 26, 2018 in Python by Nymeria
• 3,560 points
1,032 views
0 votes
1 answer

Need help with Python Text-to-Speech usage

Hi. Just before I begin my answer I ...READ MORE

answered Jan 16, 2019 in Python by Nymeria
• 3,560 points
1,044 views
0 votes
1 answer

Need help with making use of Pluck in Python

Hi, good question. Easy solution to be ...READ MORE

answered Jan 24, 2019 in Python by Nymeria
• 3,560 points
1,742 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,464 views
0 votes
1 answer
0 votes
2 answers

Need help using PYTHONPATH

PYTHONPATH only affects import statements, not the top-level Python interpreter's ...READ MORE

answered Dec 17, 2018 in Python by iangregor
• 300 points
1,168 views
0 votes
1 answer

Python using basicConfig method to log to console and file

I can't reproduce it on Python 3.3. ...READ MORE

answered Aug 14, 2018 in Python by aryya
• 7,460 points
1,231 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP