Button.grid() 在 PyCharm 的 tkinter 中无法正常工作

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

Button.grid() not working properly in pycharm tkinter

问题

这是您提供的代码,我不会翻译它。如果您需要帮助解决问题,请提出具体的问题描述,我会尽力提供指导。

英文:

I have a code that has to stack buttons, which looks like this:

  1. b1 = Button(wn, text='1', command=do_smth)
  2. b1.grid(row=0, column=0)
  3. b2 = Button(wn, text='2', command=do_smth)
  4. b2.grid(row=0, column=1)
  5. b3 = Button(wn, text='3', command=do_smth)
  6. b3.grid(row=0, column=2)
  7. b4 = Button(wn, text='4', command=do_smth)
  8. b4.grid(row=1, column=0)
  9. b5 = Button(wn, text='5', command=do_smth)
  10. b5.grid(row=1, column=1)
  11. b6 = Button(wn, text='6', command=do_smth)
  12. b6.grid(row=1, column=2)
  13. b7 = Button(wn, text='7', command=do_smth)
  14. b7.grid(row=2, column=0)
  15. b8 = Button(wn, text='8', command=do_smth)
  16. b8.grid(row=2, column=1)
  17. b9 = Button(wn, text='9', command=do_smth)
  18. b9.grid(row=2, column=2)
  19. b0 = Button(wn, text='0', command=do_smth)
  20. b0.grid(row=3, column=1)

I expectet the buttons to just place normally, but instead I got this:

  1. Traceback (most recent call last):
  2. File "D:\NUSROH AHEL\progs\calculator\main.py", line 20, in <module>
  3. btn1.grid(row=0, column=0)
  4. File "C:\Users\AlexKh\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 2485, in grid_configure
  5. self.tk.call(
  6. _tkinter.TclError: cannot use geometry manager grid inside . which already has slaves managed by pack

Can somebody please help?

(Made in Pycharm Community Edition 2022.2.2)

答案1

得分: 1

你正在文本的另一部分使用pack,只需将其替换为grid

英文:

You are using pack in the other part of the text, just replace it with grid

答案2

得分: 0

以下是您要翻译的内容:

  1. from tkinter import *
  2. def do_smth():
  3. print("按钮被按下!")
  4. wn = Tk()
  5. b1 = Button(wn, text='1', command=do_smth)
  6. b1.grid(row=0, column=0)
  7. b2 = Button(wn, text='2', command=do_smth)
  8. b2.grid(row=0, column=1)
  9. b3 = Button(wn, text='3', command=do_smth)
  10. b3.grid(row=0, column=2)
  11. b4 = Button(wn, text='4', command=do_smth)
  12. b4.grid(row=1, column=0)
  13. b5 = Button(wn, text='5', command=do_smth)
  14. b5.grid(row=1, column=1)
  15. b6 = Button(wn, text='6', command=do_smth)
  16. b6.grid(row=1, column=2)
  17. b7 = Button(wn, text='7', command=do_smth)
  18. b7.grid(row=2, column=0)
  19. b8 = Button(wn, text='8', command=do_smth)
  20. b8.grid(row=2, column=1)
  21. b9 = Button(wn, text='9', command=do_smth)
  22. b9.grid(row=2, column=2)
  23. b0 = Button(wn, text='0', command=do_smth)
  24. b0.grid(row=3, column=1)
  25. wn.mainloop()
英文:

There follow the correct way to solve your trouble:

  1. from tkinter import *
  2. def do_smth():
  3. print("Botão pressionado!")
  4. wn = Tk()
  5. b1 = Button(wn, text='1', command=do_smth)
  6. b1.grid(row=0, column=0)
  7. b2 = Button(wn, text='2', command=do_smth)
  8. b2.grid(row=0, column=1)
  9. b3 = Button(wn, text='3', command=do_smth)
  10. b3.grid(row=0, column=2)
  11. b4 = Button(wn, text='4', command=do_smth)
  12. b4.grid(row=1, column=0)
  13. b5 = Button(wn, text='5', command=do_smth)
  14. b5.grid(row=1, column=1)
  15. b6 = Button(wn, text='6', command=do_smth)
  16. b6.grid(row=1, column=2)
  17. b7 = Button(wn, text='7', command=do_smth)
  18. b7.grid(row=2, column=0)
  19. b8 = Button(wn, text='8', command=do_smth)
  20. b8.grid(row=2, column=1)
  21. b9 = Button(wn, text='9', command=do_smth)
  22. b9.grid(row=2, column=2)
  23. b0 = Button(wn, text='0', command=do_smth)
  24. b0.grid(row=3, column=1)
  25. wn.mainloop()

huangapple
  • 本文由 发表于 2023年4月10日 18:34:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75976314.html
匿名

发表评论

匿名网友

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

确定