Only returning 0.0 when entering value into entry

Question:

When entering value into entry tabs only return 0.0 for both could anyone help I’ve tried IntVar and StringVar

from tkinter import *
window = Tk()
point_label = Label(window, text="Amout of Points").pack()
point_entry = Entry(window, text="points", bd=5).pack()
Games_label = Label(window, text="Amout of games played").pack()
Games_entry = Entry(window, text="games", bd=5).pack()

def calculate ():
    Games_entry = DoubleVar()
    point_entry = DoubleVar()
    point_entry = point_entry.get()
    Games_entry = Games_entry.get()
    print(Games_entry, point_entry)

Button(window, text="Submit", command = calculate).pack()


window.geometry("800x600")
window.title('Arena Point Calculator Chpater 4 Season 2')
window.mainloop()
Asked By: LazyLenny004

||

Answers:

First, you need to declare and pack() your widgets on separate lines. pack() and the other geometry manager methods all return None. As written, point_label, point_entry and the other widgets are all equal to None which isn’t what you want (you’d end up calling None.get() which will cause an error!)

Next, you can just use get() directly on the Entry widgets. The values you get will be strings; if you need their values to be integers, you can convert them using int()

point_label = Label(window, text="Amount of Points")  # fixed typo
point_label.pack()

point_entry = Entry(window, text="points", bd=5)
point_entry.pack()

# FYI, these names should start with lowercase letters
# uppercase names are reserved for Classes and "CONSTANTS"
games_label = Label(window, text="Amount of games played")  # fixed typo
games_label.pack()

games_entry = Entry(window, text="games", bd=5)
games_entry.pack()


def calculate():
    point_entry_value = int(point_entry.get())
    games_entry_value = int(games_entry.get())
    print(point_entry_value, games_entry_value)

Lastly, it’s important to note than the calculate function isn’t doing any verification on the values, so you’ll get a ValueError exception if the values can’t be converted to integers, i.e. if you type in "Banana" or something like that.

Answered By: JRiggles

There are no minor change. Just re-arranged your code to make code readability.

Don’t use wildcard from tkinter import *

Snippet:

import tkinter as tk

window = tk.Tk()
 
Games_entry = tk.DoubleVar()
point_entry = tk.DoubleVar()
 

def calculate ():
    global Games_entry, point_entry
    point_entry = point_entry.get()
    Games_entry = Games_entry.get()
    print(Games_entry, point_entry)


point_label = tk.Label(window, text="Amout of Points").pack()
point_entry = tk.Entry(window, text="points", bd=5)
point_entry.pack()
Games_label = tk.Label(window, text="Amout of games played").pack()
Games_entry = tk.Entry(window, text="games", bd=5)
Games_entry.pack()

tk.Button(window, text="Submit", command = calculate).pack()


window.geometry("800x600")
window.title('Arena Point Calculator Chpater 4 Season 2')
window.mainloop()

Screenshot:

enter image description here

Answered By: toyota Supra
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.