columnspan in grid options dose't function

五迷三道 提交于 2021-02-08 06:59:23

问题


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()

回答1:


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:

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:



来源:https://stackoverflow.com/questions/38169515/columnspan-in-grid-options-doset-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!