Python Tkinter在按钮之间创建间距(垂直)

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

Python Tkinter making space between buttons (vertical)

问题

  1. import tkinter as tk
  2. root = tk.Tk()
  3. root.title("Calculator")
  4. def button_write():
  5. return
  6. data = tk.Entry(root, width=30, borderwidth=10, font="bold 20")
  7. data.grid(column=0, row=0, columnspan=4, padx=10, pady=10)
  8. button1 = tk.Button(root, text="1", padx=40, pady=25, command=button_write)
  9. button2 = tk.Button(root, text="2", padx=40, pady=25, command=button_write)
  10. button3 = tk.Button(root, text="3", padx=40, pady=25, command=button_write)
  11. button4 = tk.Button(root, text="4", padx=40, pady=25, command=button_write)
  12. button5 = tk.Button(root, text="5", padx=40, pady=25, command=button_write)
  13. button6 = tk.Button(root, text="6", padx=40, pady=25, command=button_write)
  14. button7 = tk.Button(root, text="7", padx=40, pady=25, command=button_write)
  15. button8 = tk.Button(root, text="8", padx=40, pady=25, command=button_write)
  16. button9 = tk.Button(root, text="9", padx=40, pady=25, command=button_write)
  17. button0 = tk.Button(root, text="0", padx=40, pady=25, command=button_write)
  18. button1.grid(row=1, column=0)
  19. button2.grid(row=1, column=1)
  20. button3.grid(row=1, column=2)
  21. button4.grid(row=1, column=3)
  22. button5.grid(row=2, column=0)
  23. button6.grid(row=2, column=1)
  24. button7.grid(row=2, column=2)
  25. button8.grid(row=2, column=3)
  26. button9.grid(row=3, column=1)
  27. button0.grid(row=3, column=2)
  28. root.mainloop()

希望这段代码对你有所帮助。如果你还有其他问题或需要进一步的帮助,请告诉我。

英文:
  1. import tkinter as tk
  2. root = tk.Tk()
  3. root.title("Calculator")
  4. def button_write():
  5. return
  6. data = tk.Entry(root, width=30, borderwidth=10, font="bold 20", )
  7. data.grid(column=0, row=0, columnspan=4, padx=10, pady=10)
  8. buton1 = tk.Button(root,text="1", padx=40, pady=25, command=button_write)
  9. buton2 = tk.Button(root,text="2", padx=40, pady=25, command=button_write)
  10. buton3 = tk.Button(root,text="3", padx=40, pady=25, command=button_write)
  11. buton4 = tk.Button(root,text="4", padx=40, pady=25, command=button_write)
  12. buton5 = tk.Button(root,text="5", padx=40, pady=25, command=button_write)
  13. buton6 = tk.Button(root,text="6", padx=40, pady=25, command=button_write)
  14. buton7 = tk.Button(root,text="7", padx=40, pady=25, command=button_write)
  15. buton8 = tk.Button(root,text="8", padx=40, pady=25, command=button_write)
  16. buton9 = tk.Button(root,text="9", padx=40, pady=25, command=button_write)
  17. buton0 = tk.Button(root,text="0", padx=40, pady=25, command=button_write)
  18. buton1.grid(row= 1,column=0)
  19. buton2.grid(row= 1,column=1)
  20. buton3.grid(row= 1,column=2)
  21. buton4.grid(row= 1,column=3)
  22. buton5.grid(row= 2,column=0)
  23. buton6.grid(row= 2,column=1)
  24. buton7.grid(row= 2,column=2)
  25. buton8.grid(row= 2,column=3)
  26. buton9.grid(row= 3,column=1)
  27. buton0.grid(row= 3,column=2)
  28. root.mainloop()

I am trying to build a calculator but I don't know how to make vertical space between buttons. I used pady for that but I didn't exactly understand how it works so I think it doesn't work for that don't worry about that 9 and 0 it is because of photo

答案1

得分: 0

需要在 grid() 调用中添加 padxpady 参数,例如:

  1. buton1.grid(row=1, column=0, padx=10, pady=10)

Button 小部件上设置填充只会设置它们的内部填充(使它们更宽/更高)

奖励回合

  1. def button_write(button):
  2. print(button) # 在这里执行你需要执行的操作
  3. for i, n in enumerate(range(9, -1, -1)):
  4. btn = tk.Button(root, text=n, command=lambda btn=n: button_write(btn))
  5. y, x = divmod(i, 3)
  6. btn.grid(row=y, column=x, padx=10, pady=10)
英文:

You need to add the padx and pady args to the grid() calls, e.g.:

  1. buton1.grid(row=1, column=0, padx=10, pady=10)

Setting the padding on the Button widgets just sets their internal padding (makes them wider / taller)

Bonus Round

  1. def button_write(button):
  2. print(button) # do whatever you need to do here
  3. for i, n in enumerate(range(9, -1, -1)):
  4. btn = tk.Button(root, text=n, command=lambda btn=n: button_write(btn))
  5. y, x = divmod(i, 3)
  6. btn.grid(row=y, column=x, padx=10, pady=10)

huangapple
  • 本文由 发表于 2023年2月18日 04:00:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75488758.html
匿名

发表评论

匿名网友

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

确定