1

Below code is my python code. I am trying to make a simple calculator with the GUI Tkinder, but here the grid system is not properly working for me plz anyone say what is the problem.

from Tkinter import *
from tkinter import font

window = Tk()
Screen = Label(window,
               text="Calculator",
               bg="#2ecc71",
               width=26,
               height=2,
               font=("Verdana", 22))

myFont = font.Font(size=12)


num7 = Button(window,
              text=7,
              height=5,
              width=10)
num7['font'] = myFont


num8 = Button(window,
              text=8,
              height=5,
              width=10)
num8['font'] = myFont

num9 = Button(window,
              text=9,
              height=5,
              width=10)
num9['font'] = myFont

window.geometry("500x500+420+120")
#window.resizable(False, False)     #commented for seeing the button that comes out of the boundary
window.title("Calculator -by Jobin")

Screen.grid(row=0, column=0, padx=10, pady=10)
num7.grid(row=1, column=0)
num8.grid(row=1, column=1)
num9.grid(row=1, column=2)

window.mainloop()

This is the output before commenting the resize

This can be seen when we try to elongate the window right

Jobin S
  • 96
  • 1
  • 9

1 Answers1

3
from tkinter import *
from tkinter import font

window = Tk()
Screen = Label(window,
               text="Calculator",
               bg="#2ecc71",
               width=26,
               height=2,
               font=("Verdana", 22))

myFont = font.Font(family='Verdana', size=36, weight='bold')


num7 = Button(window,
              text=7)
num7['font'] = myFont


num8 = Button(window,
              text=8)
num8['font'] = myFont

num9 = Button(window,
              text=9)
num9['font'] = myFont

window.geometry("500x500+420+120")
#window.resizable(False, False)     #commented for seeing the button that comes out of the boundary
window.title("Calculator -by Jobin")

Screen.grid(row=0, column=0, columnspan=3, padx=10, pady=10)
num7.grid(row=1, column=0)
num8.grid(row=1, column=1)
num9.grid(row=1, column=2)

window.mainloop()

columnspan was added. For more information look here: https://effbot.org/tkinterbook/grid.htm and here for Buttons and fonts: How to change font and size of buttons and frame in tkinter using python?

Thingamabobs
  • 7,274
  • 5
  • 21
  • 54
  • 1
    You are welcome. Check the link I posted, with the code. I learned a lot by Effbot.org – Thingamabobs Jun 28 '20 at 11:39
  • can I ask I think more sir, How to increase the font text which wants to fill the button? – Jobin S Jun 28 '20 at 11:43
  • I checked font=("Verdana",22) this model but it also increases the button size – Jobin S Jun 28 '20 at 11:45
  • 1
    Check if this answers your question: https://stackoverflow.com/questions/20588417/how-to-change-font-and-size-of-buttons-and-frame-in-tkinter-using-python – Thingamabobs Jun 28 '20 at 11:46
  • no sir the size of the button also increasing and not fill in the button – Jobin S Jun 28 '20 at 11:55
  • 1
    @JobinS updated the answer. The text in the Button widget is just a part of the Button. So if you increase the font everything around it will fit to it. So as you had configure your button width and height the space around the font was the same, but with a bigger text inside. I removed your button config for width and height. You can add some again if you want to. – Thingamabobs Jun 28 '20 at 12:05
  • 1
    "Specifies a desired width for the button. If an image or bitmap is being displayed in the button then the value is in screen units (i.e. any of the forms acceptable to Tk_GetPixels). For a text button (no image or with -compound none) then the width specifies how much space in characters to allocate for the text label. If the width is negative then this specifies a minimum width. If this option is not specified, the button's desired width is computed from the size of the image or bitmap or text being displayed in it." https://www.tcl.tk/man/tcl8.6/TkCmd/button.htm#M11 – Thingamabobs Jun 28 '20 at 12:10