Why is Tkinter widget stored as None? (AttributeError: 'NoneType' object ...)(TypeError: 'NoneType' object ...)
widget
is stored as None
because geometry manager methods grid
, pack
, place
return None
, and thus they should be called on a separate line than the line that creates an instance of the widget as in:
widget = ...
widget.grid(..)
or:
widget = ...
widget.pack(..)
or:
widget = ...
widget.place(..)
And for the 2nd code snippet in the question specifically:
widget = tkinter.Button(...).pack(...)
should be separated to two lines as:
widget = tkinter.Button(...)
widget.pack(...)
Info: This answer is based on, if not for the most parts copied from, this answer.
Python is not storing the user's input from tkinter's Entry widget into a variable
Your input is not stored in the textvariable
but in your Entry
variables. Don't use the textvariable
, but directly get the input from the entry widgets. turtle_bet = user_turtle_entry.get()
and bet_amount = user_amount_entry.get()
. You can check aftwerwards if the input was correct with an if else statement for example and even print an error if the user used a wrong input (String or Int).
Then it worked just fine for me :)
Why is Tkinter Entry's get function returning nothing?
It looks like you may be confused as to when commands are run. In your example, you are calling the get
method before the GUI has a chance to be displayed on the screen (which happens after you call mainloop
.
Try adding a button that calls the get
method. This is much easier if you write your application as a class. For example:
import tkinter as tk
class SampleApp(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.entry = tk.Entry(self)
self.button = tk.Button(self, text="Get", command=self.on_button)
self.button.pack()
self.entry.pack()
def on_button(self):
print(self.entry.get())
app = SampleApp()
app.mainloop()
Run the program, type into the entry widget, then click on the button.
Related Topics
Update Value of a Nested Dictionary of Varying Depth
What Is the Purpose of "Pip Install --User ..."
How to Avoid Circular Imports in Python
Python Beautifulsoup Parsing Table
I'm Getting Key Error in Python
Python Strings and Integer Concatenation
How to Make Smooth Movement in Pygame
Python Nameerror: Name Is Not Defined
Check for Presence of a Sliced List in Python
Python: Why Is Functools.Partial Necessary
How to Use Xpath with Beautifulsoup
Calling a Python Script with Input Within a Python Script Using Subprocess