1

I was defining a button in tkinter. then I've noticed that in options of the button's grid. changing the columnspan doesn't make any Visual change, that button remains as it used to be... http://www.tutorialspoint.com/python/tk_grid.htm Based on the definition of the columnspan... It shouldn't be like this. setting the columnspan in the grid of the last button doesn't make any change.

# myCal_Expt1.py
from tkinter import *
from decimal import *

#key press function
def click(btn_text):
    display.insert(END, btn_text)


#### main :

window = Tk()
window.title("My Calculator")

#create top_row frame

top_row = Frame(window)
top_row.grid(row=0, column=0, columnspan=2, sticky=N)

# use Entry for an editable display

display = Entry(top_row, width=45, bg= "light green")
display.grid()

#create num_pad_frame

num_pad = Frame(window)
num_pad.grid(row=1, column=0, sticky= W)

# provide a list of keys for the number pad:

num_pad_list = [
'7', '8', '9',
'4', '5', '6',
'1', '2', '3',
'0', '.', '=' ]
# create operator_frame

operator = Frame(window)
operator.grid(row=1, column=1, sticky=E)
operator_list = [ '*', '/', '+', '-', '(', ')', 'C' ]

# create operator buttons with a loop

r = 0
c = 0
for btn_text in operator_list:
    def cmd(x=btn_text):
        click(x)
    Button( operator, text=btn_text, width=3, command=cmd).grid(row=r,column=c)
    c = c + 1
    if c > 1:
       c = 0
       r = r + 1

# create num_oad buttons with a for loop

r = 0 # row counter
c = 0 # column counter

for btn_text in num_pad_list:
    def cmd(x=btn_text):
            click(x)
    Button(num_pad, text = btn_text , width=3, command=cmd).grid(row=r, column=c)
    c = c+1
    if c > 2:
        c = 0
        r = r + 1

# Adding another Frame
last_row = Frame(window)
last_row.grid(row = 2, column = 0, columnspan=2, sticky = S)

#adding another button(HERE!)

Button( last_row, text = "last", width= 20, command = click).grid(row = 5,     column = 0, columnspan = 2)

#### Run mainloop
window.mainloop()
Kian Maghsoodi
  • 123
  • 1
  • 7
  • 1
    Please provide a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) to test your problem – Billal Begueradj Jul 03 '16 at 12:21
  • why do you say it has no effect? without seeing what you have in the other columns it's impossible for us to see what the problem is. – Bryan Oakley Jul 03 '16 at 12:25
  • Are you aware that empty columns have a width of zero? What do you think spanning a zero-width column should do? – Bryan Oakley Jul 03 '16 at 14:17
  • I entered the full code. it's a calculator app. still not complete. plz only focus on the grid() mistakes (if there is any) ,why columnspan for the last button doesn't work and ignore the functioning of the app since it is not finished. – Kian Maghsoodi Jul 03 '16 at 15:27

1 Answers1

2

Inside the frame last_row you have only one button. You set this button on row=5. That is the same as setting it to row=0 because all other rows and columns are empty anyway. For the same reason, you should remove the unnecessary columnspan option for this button (as there are no columns within last_row frame to span on: they are all empty after all).

You are then spanning last_row on 2 columns. That is just fine regarding how you positioned the previous frames. However, you got the following result because the button will be drawn in the middle of the last_row by default and be as long as you set its width option to:

enter image description here

To get the button stretched over the 2 columns you expect, you simply need to set its width to the same one you did to display widget which is width=45.

In simple words, you need to change this line:

Button( last_row, text = "last", width= 20, command = click).grid(row = 5,     column = 0, columnspan = 2)

to:

Button( last_row, text="last", width=45, command=click).grid(row=0, column=0)

And you will get the result you are looking for:

enter image description here

Billal Begueradj
  • 20,717
  • 43
  • 112
  • 130