-1

I was trying to make a dice in tkinter on python and this gives me the error AttributeError: 'NoneType' object has no attribute 'configure' when i click on the button. I've not got a single clue why. btw not sure if it changes anything but im using a mac. thanks!

def Roll():
roll = 0
roll = random.randint(1,6)
Roll.configure(text = str(roll))
Roll = Button(root, text = "Roll", command = Roll, bg = "Lawn Green", fg = "Green", height = 4, width = 10).grid(row = 1, column = 6)
K.Hayden
  • 13
  • 2
  • 6
  • 1
    your indentation is incorrect, and you're using the same name for a function and for an object. – Bryan Oakley Dec 09 '15 at 21:05
  • 1
    This doesn't directly answer your question, but you should not `grid` or `pack` or `place` a widget and assign it to a variable on the same line. See [Tkinter: AttributeError: NoneType object has no attribute get](http://stackoverflow.com/q/1101750/953482) for more information. – Kevin Dec 09 '15 at 21:22
  • Actually, depending on what your indentation really looks like, maybe it _does_ directly answer your question. Hard to say without an [mcve] though. – Kevin Dec 09 '15 at 21:27

1 Answers1

2

I think I know what you're asking, here's my code below which works:

    from tkinter import *
    import random

    def Roll():
            roll = 0
            roll = random.randint(1,6)
            Roll.config(text = str(roll))

    root = Tk()
    Roll = Button(root, text = "Roll", command = Roll, bg = "Lawn Green", fg = "Green", height = 4, width = 10)
    Roll.grid(row = 1, column = 6)

    root.mainloop()

The problem was that when you call the configure to the button, you also call the grid method, which returns None. (Described here: 'NoneType' object has no attribute 'config')

So, to solve this issue, simply define the placement for the button on a different line.

Community
  • 1
  • 1
Constantly Confused
  • 595
  • 4
  • 10
  • 24