如何去除空格?tkinter

huangapple go评论81阅读模式
英文:

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(&quot;Calculator&quot;)
window.geometry(&quot;400x500&quot;)
window.resizable(False, False)

entry = tk.Entry(window, font=(&quot;Arial&quot;, 30))
entry.grid(row=0, column=0, columnspan=5, sticky=&quot;nsew&quot;)

buttons = [
    (&quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;C&quot;, &quot;/&quot;),
    (&quot;4&quot;, &quot;5&quot;, &quot;6&quot;, &quot;√&quot;, &quot;*&quot;),
    (&quot;7&quot;, &quot;8&quot;, &quot;9&quot;, &quot;&lt;-&quot;, &quot;+&quot;),
    (&quot;0&quot;, &quot;(&quot;, &quot;)&quot;, &quot;-&quot;),
    (&quot; &quot;, &quot; &quot;, &quot;.&quot;, &quot;^&quot;, &quot;=&quot;)
]

for row_idx, row in enumerate(buttons):
    for col_idx, button_text in enumerate(row):
        if button_text != &quot; &quot;:
            button = tk.Button(window, text=button_text, font=(&quot;Arial&quot;, 20), padx=3, pady=2,
                               command=lambda text=button_text: click(text))
            button.grid(row=row_idx+1, column=col_idx, sticky=&quot;nsew&quot;)

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 = [
    (&quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;C&quot;, &quot;/&quot;),
    (&quot;4&quot;, &quot;5&quot;, &quot;6&quot;, &quot;√&quot;, &quot;*&quot;),
    (&quot;7&quot;, &quot;8&quot;, &quot;9&quot;, &quot;&lt;-&quot;, &quot;+&quot;),
    (&quot;0&quot;, &quot;(&quot;, &quot;)&quot;, &quot;-&quot;, &quot;=&quot;),
    (&quot;.&quot;, None, None, &quot;^&quot;),
]

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=(&quot;Arial&quot;, 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 == &quot;.&quot; else 1,
                rowspan=2 if button_text == &quot;=&quot; else 1,
                sticky=&quot;nsew&quot;,
            )

如何去除空格?tkinter

答案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 = [
    (&quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;C&quot;, &quot;/&quot;),
    (&quot;4&quot;, &quot;5&quot;, &quot;6&quot;, &quot;&quot;, &quot;*&quot;),
    (&quot;7&quot;, &quot;8&quot;, &quot;9&quot;, &quot;&lt;-&quot;, &quot;+&quot;),
    (&quot;0&quot;, &quot;(&quot;, &quot;)&quot;, &quot;-&quot;, (&quot;=&quot;,2,1)),
    ((&quot;.&quot;,1,3), None, None, &quot;^&quot;, 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=(&quot;Arial&quot;, 20), padx=3, pady=2,
                               command=lambda text=button_text: click(text))
            button.grid(row=row_idx+1, column=col_idx, sticky=&quot;nsew&quot;, rowspan=rowspan, columnspan=colspan)

huangapple
  • 本文由 发表于 2023年7月27日 19:50:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76779443.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定