-2

I have 2 buttons https://postimg.cc/DmSQHkbG

and i want to place them next to each other

this is the code:

myButton = Button(root, text="Bitcoin ", padx=50, pady=50, command=myClick, bg="black", fg="red").pack()
#myButton.grid(row=0, column=0)
myButton2 = Button(root, text="Ethereum", padx=50, pady=50, command=myClick2, bg="gold", fg="green").pack()
#myButton2.grid(row=0, column=1)

When i run it like this with grid commented out i have the buttons below each other, when i run it with grid i get this error:

  File "button.py", line 30, in <module>
    myButton.grid(row=0, column=0)
AttributeError: 'NoneType' object has no attribute 'grid'

How to fix this?

joe blow
  • 29
  • 1
  • 6
  • When you directly use pack() at the end of the line, the myButton object is not set to the return value of the Button constructor but to the return value of pack() so it isn't a Button object. – Bongni Jan 14 '21 at 11:48

2 Answers2

0

To make the buttons the same size you can configure your columns to have the same size:

root.columnconfigure(0, weight = 1, minsize = 100)
root.columnconfigure(1, weight = 1, minsize = 100)

You have to choose between either pack() and grid(), however you are using both. You have to remove .pack() at the end of the line. So your code should look like this:

myButton = Button(root, text="Bitcoinn ", padx=50, pady=50, command=myClick, bg="black", fg="red")
myButton.grid(row=0, column=0, sticky="nsew")
myButton2 = Button(root, text="Ethereum", padx=50, pady=50, command=myClick2, bg="gold", fg="green")
myButton2.grid(row=0, column=1, sticky="nsew")
root.mainloop()

I added the sticky parameter to the grid function. This parameter decides to which side the element is aligned: n=north s=south e=east w=west. If you put all of them inside like I did: sticky="nsew" it fills out the entire grid field.

To solve the issue with myClick you have to remove .pack() once again and can write the prices in a new row:

def myClick():
    ...
    label1 = Label(root, text=str(price))
    label1.grid(row=1, column=0)
    ...

and for the other one

def myClick2():
    ...
    label2 = Label(root, text=str(price))
    label2.grid(row=1, column=1)
    ...
Bongni
  • 118
  • 2
  • 9
  • i have updated my question, i was able to fix the button size by just adding a space and make the 2 words this way the same lengths which led to same size button, however i try to have them next to each other – joe blow Jan 14 '21 at 11:47
  • Using `minsize` doesn't guarantee that the columns are the same size, only that they have the same _minimum_ size. If one of the buttons is larger than the minsize, it will cause that column to grow. The proper option to make sure two or more columns are the same size is to use the `uniform` option. – Bryan Oakley Jan 14 '21 at 14:59
0

you can place them next to each other by removing pack

myButton = Button(root, text="Bitcoinn ", padx=50, pady=50, bg="black", fg="red")
myButton.grid(row=0, column=0)
myButton2 = Button(root, text="Ethereum", padx=50, pady=50,bg="gold", fg="green")
myButton2.grid(row=0, column=1)
root.mainloop()
soham
  • 28
  • 4
  • it places it next to each othr but when i click the button the price is supposed to show up below the button: Traceback (most recent call last): File "/home/ether/anaconda3/lib/python3.7/tkinter/__init__.py", line 1705, in __call__ return self.func(*args) File "button.py", line 23, in myClick2 out = Label(root, text=str(price)).pack() File "/home/ether/anaconda3/lib/python3.7/tkinter/__init__.py", line 2143, in pack_configure + self._options(cnf, kw)) _tkinter.TclError: cannot use geometry manager pack inside . which already has slaves managed by grid – joe blow Jan 14 '21 at 11:54
  • i want it like this https://postimg.cc/nM4JBzHV, but btc price below btc button, ethereum price below eth button – joe blow Jan 14 '21 at 11:56
  • Can you please show us your myClick functions? – Bongni Jan 14 '21 at 11:57
  • https://paste.ubuntu.com/p/w66bsr82YG/ hier is the code. i want to have btc button left and eth button righ, when clickeing htem price should show up below the button, btc for btc and eth for eth – joe blow Jan 14 '21 at 11:59
  • and this is what i have currently https://postimg.cc/nM4JBzHV – joe blow Jan 14 '21 at 12:01
  • @joeblow I have updated my answer to also solve your issue with the myClick function. – Bongni Jan 14 '21 at 12:42