英文:
How to get rid of spaces ? tkinter
问题
我想把“=”按钮移到更高的位置,以覆盖上面的空间(还想把“.”变得更宽,以覆盖更多的空间)。
所以我想把位于“=”上面的单元格和位于“.”旁边的单元格合并,想要创建一个“大”按钮。
window = tk.Tk()
window.title("Calculator")
window.geometry("400x500")
window.resizable(False, False)
entry = tk.Entry(window, font=("Arial", 30))
entry.grid(row=0, column=0, columnspan=5, sticky="nsew")
buttons = [
("1", "2", "3", "C", "/"),
("4", "5", "6", "√", "*"),
("7", "8", "9", "<-", "+"),
("0", "(", ")", "-"),
(" ", " ", ".", "^", "=")
]
for row_idx, row in enumerate(buttons):
for col_idx, button_text in enumerate(row):
if button_text != " ":
button = tk.Button(window, text=button_text, font=("Arial", 20), padx=3, pady=2,
command=lambda text=button_text: click(text))
button.grid(row=row_idx+1, column=col_idx, sticky="nsew")
for i in range(5):
window.grid_columnconfigure(i, weight=1)
for i in range(len(buttons) + 1):
window.grid_rowconfigure(i, weight=1)
window.mainloop()
英文:
I want to make "=" button higher so it cover the space above(also wanna make "." wider so it also covers space)
what I am getting
so I want to join the cell above "=" and cell next to ".", wanna make one "big" button
window = tk.Tk()
window.title("Calculator")
window.geometry("400x500")
window.resizable(False, False)
entry = tk.Entry(window, font=("Arial", 30))
entry.grid(row=0, column=0, columnspan=5, sticky="nsew")
buttons = [
("1", "2", "3", "C", "/"),
("4", "5", "6", "√", "*"),
("7", "8", "9", "<-", "+"),
("0", "(", ")", "-"),
(" ", " ", ".", "^", "=")
]
for row_idx, row in enumerate(buttons):
for col_idx, button_text in enumerate(row):
if button_text != " ":
button = tk.Button(window, text=button_text, font=("Arial", 20), padx=3, pady=2,
command=lambda text=button_text: click(text))
button.grid(row=row_idx+1, column=col_idx, sticky="nsew")
for i in range(5):
window.grid_columnconfigure(i, weight=1)
for i in range(len(buttons) + 1):
window.grid_rowconfigure(i, weight=1)
window.mainloop()
答案1
得分: 2
# 将 `columnspan` 和 `rowspan` 设置为相应的值。
buttons = [
("1", "2", "3", "C", "/"),
("4", "5", "6", "√", "*"),
("7", "8", "9", "<-", "+"),
("0", "(", ")", "-", "="),
(".", None, None, "^"),
]
for row_idx, row in enumerate(buttons):
for col_idx, button_text in enumerate(row):
if button_text:
button = tk.Button(
window,
text=button_text,
font=("Arial", 20),
padx=3,
pady=2,
command=lambda text=button_text: click(text),
)
button.grid(
row=row_idx + 1,
column=col_idx,
columnspan=3 if button_text == "." else 1,
rowspan=2 if button_text == "=" else 1,
sticky="nsew",
)
英文:
Set columnspan
and rowspan
accordingly.
This has them hard-coded based on the button texts that should span, but you can probably do something more elegant.
buttons = [
("1", "2", "3", "C", "/"),
("4", "5", "6", "√", "*"),
("7", "8", "9", "<-", "+"),
("0", "(", ")", "-", "="),
(".", None, None, "^"),
]
for row_idx, row in enumerate(buttons):
for col_idx, button_text in enumerate(row):
if button_text:
button = tk.Button(
window,
text=button_text,
font=("Arial", 20),
padx=3,
pady=2,
command=lambda text=button_text: click(text),
)
button.grid(
row=row_idx + 1,
column=col_idx,
columnspan=3 if button_text == "." else 1,
rowspan=2 if button_text == "=" else 1,
sticky="nsew",
)
答案2
得分: 0
一种通用的方法是使用:
- 一个元组来定义 (
text
,rowspan
,columnspan
) 按钮的选项,或者 - 一个字符串来定义
text
选项, - 对于被其他按钮占用的单元格,使用
None
buttons = [
("1", "2", "3", "C", "/"),
("4", "5", "6", "√", "*"),
("7", "8", "9", "<-", "+"),
("0", "(", ")", "-", ("=", 2, 1)),
((".", 1, 3), None, None, "^", None)
]
for row_idx, row in enumerate(buttons):
for col_idx, button_text in enumerate(row):
if button_text:
rowspan = colspan = 1
if type(button_text) is tuple:
button_text, rowspan, colspan = button_text
button = tk.Button(window, text=button_text, font=("Arial", 20), padx=3, pady=2,
command=lambda text=button_text: click(text))
button.grid(row=row_idx+1, column=col_idx, sticky="nsew", rowspan=rowspan, columnspan=colspan)
英文:
One generic way is using:
- a
tuple
to define (text
,rowspan
,columnspan
) options for the button, or - a string to define the
text
option only None
for cell that is occupied by other button
buttons = [
("1", "2", "3", "C", "/"),
("4", "5", "6", "√", "*"),
("7", "8", "9", "<-", "+"),
("0", "(", ")", "-", ("=",2,1)),
((".",1,3), None, None, "^", None)
]
for row_idx, row in enumerate(buttons):
for col_idx, button_text in enumerate(row):
if button_text:
rowspan = colspan = 1
if type(button_text) is tuple:
button_text, rowspan, colspan = button_text
button = tk.Button(window, text=button_text, font=("Arial", 20), padx=3, pady=2,
command=lambda text=button_text: click(text))
button.grid(row=row_idx+1, column=col_idx, sticky="nsew", rowspan=rowspan, columnspan=colspan)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论