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

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

Button.grid() not working properly in pycharm tkinter

问题

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

英文:

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

b1 = Button(wn, text='1', command=do_smth)
b1.grid(row=0, column=0)
b2 = Button(wn, text='2', command=do_smth)
b2.grid(row=0, column=1)
b3 = Button(wn, text='3', command=do_smth)
b3.grid(row=0, column=2)
b4 = Button(wn, text='4', command=do_smth)
b4.grid(row=1, column=0)
b5 = Button(wn, text='5', command=do_smth)
b5.grid(row=1, column=1)
b6 = Button(wn, text='6', command=do_smth)
b6.grid(row=1, column=2)
b7 = Button(wn, text='7', command=do_smth)
b7.grid(row=2, column=0)
b8 = Button(wn, text='8', command=do_smth)
b8.grid(row=2, column=1)
b9 = Button(wn, text='9', command=do_smth)
b9.grid(row=2, column=2)
b0 = Button(wn, text='0', command=do_smth)
b0.grid(row=3, column=1)

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

Traceback (most recent call last):
  File "D:\NUSROH AHEL\progs\calculator\main.py", line 20, in <module>
    btn1.grid(row=0, column=0)
  File "C:\Users\AlexKh\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 2485, in grid_configure
    self.tk.call(
_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

以下是您要翻译的内容:

from tkinter import *

def do_smth():
    print("按钮被按下!")

wn = Tk()

b1 = Button(wn, text='1', command=do_smth)
b1.grid(row=0, column=0)

b2 = Button(wn, text='2', command=do_smth)
b2.grid(row=0, column=1)

b3 = Button(wn, text='3', command=do_smth)
b3.grid(row=0, column=2)

b4 = Button(wn, text='4', command=do_smth)
b4.grid(row=1, column=0)

b5 = Button(wn, text='5', command=do_smth)
b5.grid(row=1, column=1)

b6 = Button(wn, text='6', command=do_smth)
b6.grid(row=1, column=2)

b7 = Button(wn, text='7', command=do_smth)
b7.grid(row=2, column=0)

b8 = Button(wn, text='8', command=do_smth)
b8.grid(row=2, column=1)

b9 = Button(wn, text='9', command=do_smth)
b9.grid(row=2, column=2)

b0 = Button(wn, text='0', command=do_smth)
b0.grid(row=3, column=1)

wn.mainloop()
英文:

There follow the correct way to solve your trouble:

from tkinter import *

def do_smth():
    print("Botão pressionado!")

wn = Tk()

b1 = Button(wn, text='1', command=do_smth)
b1.grid(row=0, column=0)

b2 = Button(wn, text='2', command=do_smth)
b2.grid(row=0, column=1)

b3 = Button(wn, text='3', command=do_smth)
b3.grid(row=0, column=2)

b4 = Button(wn, text='4', command=do_smth)
b4.grid(row=1, column=0)

b5 = Button(wn, text='5', command=do_smth)
b5.grid(row=1, column=1)

b6 = Button(wn, text='6', command=do_smth)
b6.grid(row=1, column=2)

b7 = Button(wn, text='7', command=do_smth)
b7.grid(row=2, column=0)

b8 = Button(wn, text='8', command=do_smth)
b8.grid(row=2, column=1)

b9 = Button(wn, text='9', command=do_smth)
b9.grid(row=2, column=2)

b0 = Button(wn, text='0', command=do_smth)
b0.grid(row=3, column=1)

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:

确定