网格选项中的 columnspan 不起作用 [英] columnspan in grid options dose't function

查看:27
本文介绍了网格选项中的 columnspan 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 tkinter 中定义了一个按钮.然后我注意到在按钮网格的选项中.更改 columnspan 不会造成任何视觉上的变化,该按钮保持原样......

要将按钮拉伸到您期望的 2 列上,您只需将其宽度设置为与 display 小部件相同的宽度,即 width=45.

简单来说,你需要改变这一行:

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

到:

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

你会得到你想要的结果:

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

解决方案

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:

这篇关于网格选项中的 columnspan 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆