1

Whenever I try to move individual widgets or click a button that produces words effectively moving other widgets that had nothing to do those both specified actions above.

Here is my code:

import tkinter as tk

# Create the main window
window = tk.Tk()
window.title("Price Calculator")
window.geometry("800x600")
window.config(background = "#777474")

def calculate_price():
    
    global input_field
    input_field_value = input_field.get()

    try:
        input = int(input_field_value)
        price = input* 0.1
        answer.config(text = (f"Your price is ","%.3f"%price,"KWD"))
    except ValueError as ve:
        answer.config(text = 'What you have just entered are not numbers whatsoever, try again!', fg = "#CD5F66")       

price_input = tk.Label(window, text = "Total Pages:", font = "Arial", bg = "#777474", fg = "#FEFCF2")
price_input.grid(column = 0, row = 0)

input_field = tk.Entry(window, font = "Arial", bg = "#FEFCF2")
input_field.grid(column = 1, row = 0, padx = 0,pady = 10)
   

answer = tk.Label(window, bg = "#777474")
answer.grid(pady = 20)

button_return = tk.Button(window, text = "Calculate Price", command = calculate_price).grid()


# Run the main loop
window.mainloop()

This is my GUI before I click on the button which is called "Calculate Price":

This is my GUI before I click on the button which is called "Calculate Price"

This is my GUI after I have clicked on the button:

This is my GUI after I have clicked on the button

The problem here is that I don't want the Total Pages and the entry field to move away from each other whenever I click on the button.

Anton Menshov
  • 2,266
  • 14
  • 34
  • 55
  • 1
    This is the *normal* behavior. You have a grid cells that get bigger and smaller with the content in the cell. I wrote a quick [overview](https://stackoverflow.com/a/63536506/13629335) over tkinter basic geometry, if you are interested. I would also suggest you to use `answer.grid(pady = 20, columnspan=2)` which seems to fit for you. – Thingamabobs Dec 22 '22 at 16:38

1 Answers1

0

I would suggest to put price_input and input_field into a frame for easier layout management. I also use sticky="w" on the frame, answer and button_return so that they are aligned at the left.

Below is the modified code:

import tkinter as tk

FGCOLOR = "#FEFCF2"
BGCOLOR = "#777474"
ERRCOLOR = "#FF5F66"

# Create the main window
window = tk.Tk()
window.title("Price Calculator")
window.geometry("800x600")
window.config(background="#777474")

def calculate_price():
    input_field_value = input_field.get()
    try:
        input = int(input_field_value)
        price = input * 0.1
        answer.config(text=f"Your price is {price:.3f} KWD", fg=FGCOLOR)
    except ValueError as ve:
        answer.config(text='What you have just entered are not numbers whatsoever, try again!', fg=ERRCOLOR)

# create a frame for price_input and input_field
frame = tk.Frame(window, bg=BGCOLOR)
frame.grid(column=0, row=0, padx=10, pady=10, sticky="w")

price_input = tk.Label(frame, text="Total Pages:", font="Arial", bg=BGCOLOR, fg=FGCOLOR)
price_input.grid(column=0, row=0)

input_field = tk.Entry(frame, font="Arial", bg=FGCOLOR)
input_field.grid(column=1, row=0)

answer = tk.Label(window, bg=BGCOLOR)
answer.grid(column=0, row=1, padx=10, pady=10, sticky="w")

button_return = tk.Button(window, text="Calculate Price", command=calculate_price)
button_return.grid(column=0, row=2, sticky="w", padx=10, pady=10)

# Run the main loop
window.mainloop()
acw1668
  • 40,144
  • 5
  • 22
  • 34