user15022116
user15022116

Reputation:

Not able to adjust the size of the application in the Tkinter

from tkinter import *
import tkinter.messagebox as tmsg
root = Tk()
root.title("Temperater Converter")
root.wm_iconbitmap("Temperature.ico")
root.geometry("499x500")
    
# This function will convert Celcius to fahrenheit
    
    
def Convert():
    if FahOrCel.get() == 1:
        Celsius = Temperature.get()
        Fahrenheit = (float(Celcius)*9/5 + 32)
        tmsg.showinfo("Conversion Succsfull!",
                      f"{Celsius} Celcius is {Fahrenheit} Fahrenheit")
# This function will convert fahrenheit to celcius
    elif FahOrCel2.get() == 1:
        Fahrenheit = Temperature.get()
        Celsius = (float(Fahrenheit) - 32) / 1.8
        tmsg.showinfo("Conversion Successfully!",
                      f"{Fahrenheit} Fahrenheit is {Celsius} Celsius")
    

# Main Heading of the program
# font="lucida 20 bold"
frame = Frame(root, bg="yellow", borderwidth=10, relief=SUNKEN).grid(row=1, column=3)
# Txt = Label(frame,text="Temperature Converter",fg="green", font="comicsansms 10 bold",padx=15).grid(row=0,column=3)
    
Txt1 = Label(text="Temperature", font="Cascadia 20").grid(row=4, column=1)
Temperature = IntVar()
TemEnt = Entry(root, textvariable=Temperature, width=10,
               font="Cascadia 15 bold").grid(row=4, column=2, padx=5)
    
FahOrCel = IntVar()
Temp = Checkbutton(text="Celcius", variable=FahOrCel).grid(row=4, column=3)
FahOrCel2 = IntVar()
Temp2 = Checkbutton(text="Fahrenheit",
                    variable=FahOrCel2).grid(row=4, column=4)
    
convert = Button(root, text="Convert It!", font="Cascadia 15 bold",
                 command=Convert).grid(row=15, column=2)
    
root.mainloop()

When we run this code the GUI is not aligned properly and when we add some function or do some modifications then also the alignment of the GUI is not perfect. If we change it with any method the alignment is not correct then also.

Upvotes: 0

Views: 88

Answers (1)

AKX
AKX

Reputation: 168861

In order to make the layout stretchy, you'll need to use rowconfigure and columnconfigure to set a weight on the columns. You'll also want to make sure you've added padding so the layout isn't all cramped up.

I also

  • fixed some typos
  • fixed imports not to use * (it's good hygiene, so you know where your values come from)
  • removed the unused yellow frame
  • removed references to fonts and icons I don't have
  • simplified the logic in the conversion function
  • swapped the checkboxes for radio buttons (which are what you'd usually use for a mutually exclusive group of selections)
  • normalized the grid column/row references so they're not e.g. row=15 (there's only 2 logical rows of components, anyway)
from tkinter import Tk, Label, IntVar, Entry, Button, Radiobutton
from tkinter.messagebox import showinfo


def Convert():
    mode = FahOrCel.get()
    temperature_value = Temperature.get()
    if mode == 1:
        fahrenheit = float(temperature_value) * 9 / 5 + 32
        showinfo(message=f"{temperature_value} Celsius is {fahrenheit} Fahrenheit")
    elif mode == 2:
        celsius = (float(temperature_value) - 32) / 1.8
        showinfo(message=f"{temperature_value} Fahrenheit is {celsius} Celsius")


root = Tk()
root.title("Temperature Converter")
root.columnconfigure([1, 2, 3], weight=1, pad=10)
root.rowconfigure([1, 2], weight=1, pad=10)


Label(text="Temperature").grid(row=1, column=1)
Temperature = IntVar()
Entry(root, textvariable=Temperature, width=10).grid(row=1, column=2, padx=5, sticky="ew")

FahOrCel = IntVar(value=1)
Radiobutton(root, text="Celcius", value=1, variable=FahOrCel).grid(row=1, column=3, sticky="ew", padx=5)
Radiobutton(root, text="Fahrenheit", value=2, variable=FahOrCel).grid(row=1, column=4, sticky="ew", padx=5)

Button(root, text="Convert It!", command=Convert).grid(row=2, column=1, columnspan=4, sticky="news", padx=5, pady=5)

root.mainloop()

The result is, by default,

enter image description here

but you can stretch it to e.g.

enter image description here

Upvotes: 1

Related Questions