How to make tkinter button widget take up full width of grid

2024/10/3 12:40:24

I've tried this but it didn't help.

I'm making a calculator program. I've made this so far:

from tkinter import *
window = Tk()disp = Entry(window, state='readonly', readonlybackground="white")
disp.grid(column=0, row=0, columnspan=4)
#row 1
seven = Button(window, text="7", command=Seven)
seven.grid(column=1,row=1)eight = Button(window, text="8", command=Eight)
eight.grid(column=2,row=1)nine = Button(window, text="9", command=Nine)
nine.grid(column=3,row=1)divide = Button(window, text="÷", command=Divide)
divide.grid(column=4,row=1)#row 2four = Button(window, text="4", command=Four)
four.grid(column=1,row=2)five = Button(window, text="5", command=Five)
five.grid(column=2,row=2)six = Button(window, text="6", command=Six)
six.grid(column=3,row=2)multiply = Button(window, text="×", command=Multiply)
multiply.grid(column=4,row=2)#row 3one = Button(window, text="1", command=One)
one.grid(column=1,row=3)two = Button(window, text="2", command=Two)
two.grid(column=2,row=3)three = Button(window, text="3", command=Three)
three.grid(column=3,row=3)minus = Button(window, text="-", command=Minus)
minus.grid(column=4,row=3)#row 4zero = Button(window, text="0", command=Zero)
zero.grid(column=1,row=4)dec = Button(window, text=".", command=Dec)
dec.grid(column=2,row=4)equal = Button(window, text="=", command=Equal)
equal.grid(column=3,row=4)add = Button(window, text="+", command=Add)
add.grid(column=4,row=4)window.mainloop()

This looks like this:
Calculator with broken buttons

I'd like the boxes to be equally wide and fill the available space. The result should look similar to this:
Calculator with fixed buttons

How do you make a button take up the entire width of the row/column?

Answer

Two things:

  1. You set your entry box to apply from column 0 onwards, but then each subsequent row operates from column 1 onwards. Be consistent in this - your button for 7 should be in column 0, 8 in 1 etc.
  2. When you .grid your buttons, use sticky=N+S+E+W. This will allow the buttons to expand with their respective row and column sizes.

Update: N+S+E+W is not working for python3.6.7 it is neswor any combination of these 4 letters.

For example:

#!/usr/bin/python
# -*- coding: utf-8 -*-from tkinter import *
window = Tk()disp = Entry(window, state='readonly', readonlybackground="white")
disp.grid(column=0, row=0, columnspan=4)
#row 1
seven = Button(window, text="7")
seven.grid(column=0,row=1, sticky='nesw')eight = Button(window, text="8")
eight.grid(column=1,row=1, sticky='nesw')nine = Button(window, text="9")
nine.grid(column=2,row=1, sticky='nesw')divide = Button(window, text="÷")
divide.grid(column=3,row=1, sticky='nesw')window.mainloop()

returns a window that looks like:

Tkinter buttons with sticky

https://en.xdnf.cn/q/70725.html

Related Q&A

Python strip() unicode string?

How can you use string methods like strip() on a unicode string? and cant you access characters of a unicode string like with oridnary strings? (ex: mystring[0:4] )

Python equivalent for MATLABs normplot?

Is there a python equivalent function similar to normplot from MATLAB? Perhaps in matplotlib?MATLAB syntax:x = normrnd(10,1,25,1); normplot(x)Gives:I have tried using matplotlib & numpy module to…

python mask netcdf data using shapefile

I am using the following packages:import pandas as pd import numpy as np import xarray as xr import geopandas as gpdI have the following objects storing data:print(precip_da)Out[]:<xarray.DataArray …

Whats a good general way to look SQLAlchemy transactions, complete with authenticated user, etc?

Im using SQLAlchemys declarative extension. Id like all changes to tables logs, including changes in many-to-many relationships (mapping tables). Each table should have a separate "log" table…

OpenCV - Tilted camera and triangulation landmark for stereo vision

I am using a stereo system and so I am trying to get world coordinates of some points by triangulation.My cameras present an angle, the Z axis direction (direction of the depth) is not normal to my sur…

Node.jss python child script outputting on finish, not real time

I am new to node.js and socket.io and I am trying to write a small server that will update a webpage based on python output. Eventually this will be used for a temperature sensor so for now I have a du…

lambda function returning the key value for use in defaultdict

The function collections.defaultdict returns a default value that can be defined by a lambda function of my own making if the key is absent from my dictionary.Now, I wish my defaultdict to return the u…

Calling Matlab function from python

I have one project in which I have one one matlab code which I have to run tho Django. I tried installing Mlabwrap ..But it gives me following error.Traceback (most recent call last): File "<st…

Suds ignoring proxy setting

Im trying to use the salesforce-python-toolkit to make web services calls to the Salesforce API, however Im having trouble getting the client to go through a proxy. Since the toolkit is based on top of…

CSV to JSON script

I took this script from here: import csv from itertools import izip f = open( /django/sw2/wkw2/csvtest1.csv, r ) reader = csv.reader( f ) keys = ( "firm_url", "firm_name", "fir…