Learning Python- Intermediate course: Day 31, Coordinate positions

Today we will cover the coordinate layout in Tkinter

Today we will make a program which will take in inputs of principle and discount and calculate the total price.

Here are the program specs

One label for the text principle
One lab…



Today we will cover the coordinate layout in Tkinter

Today we will make a program which will take in inputs of principle and discount and calculate the total price.

image

Here are the program specs

  • One label for the text principle
  • One label for the text discount
  • One label for the total value
  • One button show.
  • One slider for the discount values
  • One spinbox for the principle amount

Now that we have got all the specs, let’s start building the program-

from tkinter import *
master=Tk()
master.geometry("300x200")

Lbl1=Label(master,text="Principle")
Lbl2=Label(master,text="Discount")
Lbl3=Label(master,text="Total. ",font="20px")
spinbox=Spinbox(master,from_=10, to=100)
slider=Scale(master,from_=0, to=100, tickinterval=20, length= 150, orient="horizontal")
def display():
    a=int(spinbox.get())-int(spinbox.get())*int(slider.get())/100
    Lbl3.config(text=str(a)+"$")
button=Button(master,text="Calculate",command=display)

spinbox.pack()
Lbl1.pack()
slider.pack()
Lbl2.pack()
button.pack()
Lbl3.pack()

mainloop()


image
image

The program works all right, but that’s not how we want to display the widgets. We need to adjust the look and feel. We want the first two labels to be adjacent towards the spinbox and slider widgets. For that, we use the coordinate layout. The pack layout is not sufficient as it packs all the widgets into just a centre line. Hence, we will place them coordinately.

We can set the coordinates of the widgets using the place method. Example widget.place(x=30,y=20)
Here is the final program, now with the power of place layout.


from tkinter import *
master=Tk()
master.geometry("300x200")

Lbl1=Label(master,text="Principle")
Lbl2=Label(master,text="Discount")
Lbl3=Label(master,text="Total. ",font="20px")
spinbox=Spinbox(master,from_=10, to=100)
slider=Scale(master,from_=0, to=100, tickinterval=20, length= 150, orient="horizontal")
def display():
    a=int(spinbox.get())-int(spinbox.get())*int(slider.get())/100
    Lbl3.config(text=str(a)+"$")
button=Button(master,text="Calculate",command=display)

spinbox.place(x=100, y=0)
Lbl1.place(x=0,y=0)
slider.place(x=100, y=50)
Lbl2.place(x=0,y=70)
button.place(x=150,y=130)
Lbl3.place(x=150,y=170)

mainloop()

image

image

One thing to note is that the layouts remain the same even if the window is resized.

image


Print Share Comment Cite Upload Translate
APA
Aatmaj | Sciencx (2024-03-29T00:08:09+00:00) » Learning Python- Intermediate course: Day 31, Coordinate positions. Retrieved from https://www.scien.cx/2021/09/24/learning-python-intermediate-course-day-31-coordinate-positions/.
MLA
" » Learning Python- Intermediate course: Day 31, Coordinate positions." Aatmaj | Sciencx - Friday September 24, 2021, https://www.scien.cx/2021/09/24/learning-python-intermediate-course-day-31-coordinate-positions/
HARVARD
Aatmaj | Sciencx Friday September 24, 2021 » Learning Python- Intermediate course: Day 31, Coordinate positions., viewed 2024-03-29T00:08:09+00:00,<https://www.scien.cx/2021/09/24/learning-python-intermediate-course-day-31-coordinate-positions/>
VANCOUVER
Aatmaj | Sciencx - » Learning Python- Intermediate course: Day 31, Coordinate positions. [Internet]. [Accessed 2024-03-29T00:08:09+00:00]. Available from: https://www.scien.cx/2021/09/24/learning-python-intermediate-course-day-31-coordinate-positions/
CHICAGO
" » Learning Python- Intermediate course: Day 31, Coordinate positions." Aatmaj | Sciencx - Accessed 2024-03-29T00:08:09+00:00. https://www.scien.cx/2021/09/24/learning-python-intermediate-course-day-31-coordinate-positions/
IEEE
" » Learning Python- Intermediate course: Day 31, Coordinate positions." Aatmaj | Sciencx [Online]. Available: https://www.scien.cx/2021/09/24/learning-python-intermediate-course-day-31-coordinate-positions/. [Accessed: 2024-03-29T00:08:09+00:00]
rf:citation
» Learning Python- Intermediate course: Day 31, Coordinate positions | Aatmaj | Sciencx | https://www.scien.cx/2021/09/24/learning-python-intermediate-course-day-31-coordinate-positions/ | 2024-03-29T00:08:09+00:00
https://github.com/addpipe/simple-recorderjs-demo