如何去除空格?tkinter

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

How to get rid of spaces ? tkinter

问题

我想把“=”按钮移到更高的位置,以覆盖上面的空间(还想把“.”变得更宽,以覆盖更多的空间)。

我得到的效果

所以我想把位于“=”上面的单元格和位于“.”旁边的单元格合并,想要创建一个“大”按钮。

  1. window = tk.Tk()
  2. window.title("Calculator")
  3. window.geometry("400x500")
  4. window.resizable(False, False)
  5. entry = tk.Entry(window, font=("Arial", 30))
  6. entry.grid(row=0, column=0, columnspan=5, sticky="nsew")
  7. buttons = [
  8. ("1", "2", "3", "C", "/"),
  9. ("4", "5", "6", "√", "*"),
  10. ("7", "8", "9", "<-", "+"),
  11. ("0", "(", ")", "-"),
  12. (" ", " ", ".", "^", "=")
  13. ]
  14. for row_idx, row in enumerate(buttons):
  15. for col_idx, button_text in enumerate(row):
  16. if button_text != " ":
  17. button = tk.Button(window, text=button_text, font=("Arial", 20), padx=3, pady=2,
  18. command=lambda text=button_text: click(text))
  19. button.grid(row=row_idx+1, column=col_idx, sticky="nsew")
  20. for i in range(5):
  21. window.grid_columnconfigure(i, weight=1)
  22. for i in range(len(buttons) + 1):
  23. window.grid_rowconfigure(i, weight=1)
  24. 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

  1. window = tk.Tk()
  2. window.title(&quot;Calculator&quot;)
  3. window.geometry(&quot;400x500&quot;)
  4. window.resizable(False, False)
  5. entry = tk.Entry(window, font=(&quot;Arial&quot;, 30))
  6. entry.grid(row=0, column=0, columnspan=5, sticky=&quot;nsew&quot;)
  7. buttons = [
  8. (&quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;C&quot;, &quot;/&quot;),
  9. (&quot;4&quot;, &quot;5&quot;, &quot;6&quot;, &quot;√&quot;, &quot;*&quot;),
  10. (&quot;7&quot;, &quot;8&quot;, &quot;9&quot;, &quot;&lt;-&quot;, &quot;+&quot;),
  11. (&quot;0&quot;, &quot;(&quot;, &quot;)&quot;, &quot;-&quot;),
  12. (&quot; &quot;, &quot; &quot;, &quot;.&quot;, &quot;^&quot;, &quot;=&quot;)
  13. ]
  14. for row_idx, row in enumerate(buttons):
  15. for col_idx, button_text in enumerate(row):
  16. if button_text != &quot; &quot;:
  17. button = tk.Button(window, text=button_text, font=(&quot;Arial&quot;, 20), padx=3, pady=2,
  18. command=lambda text=button_text: click(text))
  19. button.grid(row=row_idx+1, column=col_idx, sticky=&quot;nsew&quot;)
  20. for i in range(5):
  21. window.grid_columnconfigure(i, weight=1)
  22. for i in range(len(buttons) + 1):
  23. window.grid_rowconfigure(i, weight=1)
  24. window.mainloop()

答案1

得分: 2

  1. # 将 `columnspan` 和 `rowspan` 设置为相应的值。
  2. buttons = [
  3. ("1", "2", "3", "C", "/"),
  4. ("4", "5", "6", "√", "*"),
  5. ("7", "8", "9", "<-", "+"),
  6. ("0", "(", ")", "-", "="),
  7. (".", None, None, "^"),
  8. ]
  9. for row_idx, row in enumerate(buttons):
  10. for col_idx, button_text in enumerate(row):
  11. if button_text:
  12. button = tk.Button(
  13. window,
  14. text=button_text,
  15. font=("Arial", 20),
  16. padx=3,
  17. pady=2,
  18. command=lambda text=button_text: click(text),
  19. )
  20. button.grid(
  21. row=row_idx + 1,
  22. column=col_idx,
  23. columnspan=3 if button_text == "." else 1,
  24. rowspan=2 if button_text == "=" else 1,
  25. sticky="nsew",
  26. )
英文:

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.

  1. buttons = [
  2. (&quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;C&quot;, &quot;/&quot;),
  3. (&quot;4&quot;, &quot;5&quot;, &quot;6&quot;, &quot;√&quot;, &quot;*&quot;),
  4. (&quot;7&quot;, &quot;8&quot;, &quot;9&quot;, &quot;&lt;-&quot;, &quot;+&quot;),
  5. (&quot;0&quot;, &quot;(&quot;, &quot;)&quot;, &quot;-&quot;, &quot;=&quot;),
  6. (&quot;.&quot;, None, None, &quot;^&quot;),
  7. ]
  8. for row_idx, row in enumerate(buttons):
  9. for col_idx, button_text in enumerate(row):
  10. if button_text:
  11. button = tk.Button(
  12. window,
  13. text=button_text,
  14. font=(&quot;Arial&quot;, 20),
  15. padx=3,
  16. pady=2,
  17. command=lambda text=button_text: click(text),
  18. )
  19. button.grid(
  20. row=row_idx + 1,
  21. column=col_idx,
  22. columnspan=3 if button_text == &quot;.&quot; else 1,
  23. rowspan=2 if button_text == &quot;=&quot; else 1,
  24. sticky=&quot;nsew&quot;,
  25. )

如何去除空格?tkinter

答案2

得分: 0

一种通用的方法是使用:

  • 一个元组来定义 (text, rowspan, columnspan) 按钮的选项,或者
  • 一个字符串来定义 text 选项,
  • 对于被其他按钮占用的单元格,使用 None
  1. buttons = [
  2. ("1", "2", "3", "C", "/"),
  3. ("4", "5", "6", "√", "*"),
  4. ("7", "8", "9", "<-", "+"),
  5. ("0", "(", ")", "-", ("=", 2, 1)),
  6. ((".", 1, 3), None, None, "^", None)
  7. ]
  8. for row_idx, row in enumerate(buttons):
  9. for col_idx, button_text in enumerate(row):
  10. if button_text:
  11. rowspan = colspan = 1
  12. if type(button_text) is tuple:
  13. button_text, rowspan, colspan = button_text
  14. button = tk.Button(window, text=button_text, font=("Arial", 20), padx=3, pady=2,
  15. command=lambda text=button_text: click(text))
  16. 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
  1. buttons = [
  2. (&quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;C&quot;, &quot;/&quot;),
  3. (&quot;4&quot;, &quot;5&quot;, &quot;6&quot;, &quot;&quot;, &quot;*&quot;),
  4. (&quot;7&quot;, &quot;8&quot;, &quot;9&quot;, &quot;&lt;-&quot;, &quot;+&quot;),
  5. (&quot;0&quot;, &quot;(&quot;, &quot;)&quot;, &quot;-&quot;, (&quot;=&quot;,2,1)),
  6. ((&quot;.&quot;,1,3), None, None, &quot;^&quot;, None)
  7. ]
  8. for row_idx, row in enumerate(buttons):
  9. for col_idx, button_text in enumerate(row):
  10. if button_text:
  11. rowspan = colspan = 1
  12. if type(button_text) is tuple:
  13. button_text, rowspan, colspan = button_text
  14. button = tk.Button(window, text=button_text, font=(&quot;Arial&quot;, 20), padx=3, pady=2,
  15. command=lambda text=button_text: click(text))
  16. 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:

确定