Python tkinters entry.get() does not work, how can I fix it? [duplicate]

2024/10/12 22:32:13

I am building a simple program for university. We have to convert our code to an interface. Ive managed to make the interface, but i cant seem to pass my values from Entry to the actual code. Here is my code:

import sys
from tkinter import *
from tkinter import ttk
import time
from datetime import datetime
now= datetime.now()d = dict()
def quit():print("Have a great day! Goodbye :)")sys.exit(0)
def display():print(str(d))
def add(*args): global stockglobal dstock = stock_Entry.get()Quantity = Quantity_Entry.get()if stock not in d:d[stock] = Quantityelse:d[stock] += Quantityroot = Tk()
root.title("Homework 5 216020088")mainframe = ttk.Frame(root, padding="6 6 20 20")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))ttk.Label(mainframe, text="you are accesing this on day %s of month %s of %s" % (now.day,now.month,now.year)+" at exactly %s:%s:%s" % (now.hour,now.minute,now.second), foreground="yellow", background="Black").grid(column=0, row = 0)stock_Entry= ttk.Entry(mainframe, width = 60, textvariable="stock").grid(column=0, row = 1, sticky=W)
ttk.Label(mainframe, text="Please enter the stock name").grid(column=1, row = 1, sticky=(N, W, E, S))Quantity_Entry= ttk.Entry(mainframe, width = 60, textvariable="Quantity").grid(column=0, row = 2, sticky=W)
ttk.Label(mainframe, text="Please enter the quantity").grid(column=1, row = 2, sticky=(N, W, E, S))ttk.Button(mainframe, text="Add", command=add).grid(column=0, row=3, sticky=W)
ttk.Button(mainframe, text="Display", command=display).grid(column=0, row=3, sticky=S)
ttk.Button(mainframe, text="Exit", command=quit).grid(column=0, row=3, sticky=E)for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5)
root.mainloop()
Answer

You cannot create the widget and grid it at the same time like this, if you still need to access it later. stock_Entry.get() will raise an AttributeError (NoneType).

Instead of:

stock_Entry= ttk.Entry(mainframe, width = 60, textvariable="stock").grid(column=0, row = 1, sticky=W)

use:

stock_Entry = ttk.Entry(mainframe, width = 60, textvariable="stock")
stock_Entry.grid(column=0, row = 1, sticky=W)

Same for the Quantity_Entry:

Quantity_Entry = ttk.Entry(mainframe, width = 60, textvariable="Quantity")
Quantity_Entry.grid(column=0, row = 2, sticky=W)

...and it works:

enter image description here

https://en.xdnf.cn/q/118151.html

Related Q&A

Pandas secondary y axis for boxplots

Id like to use a secondary y-axis for some boxplots in pandas, but it doesnt seem available. import numpy as np import pandas as pddata = np.random.random((10, 5)) data[:,-1] += 10 # offset one column…

Fixing Negative Assertion for end of string

I am trying to accept a capture group only if the pattern matches and there is not a specific word before the end of the group. Ive tried a # of approaches and none seem to work, clearly Im not getting…

Two Sorted Arrays, sum of 2 elements equal a certain number

I was wondering if I could get some help. I want to find an algorithm that is THETA(n) or linear time for determining whether 2 numbers in a 2 sorted arrays add up to a certain number.For instance, let…

I cant seem to install numpy

I tried to install numpy, but whenever I start my program, I get these messages.Error importing numpy: you should not try to import numpy fromits source directory; please exit the numpy source tree, an…

Using slices in Python

I use the dataset from UCI repo: http://archive.ics.uci.edu/ml/datasets/Energy+efficiency Then doing next:from pandas import * from sklearn.neighbors import KNeighborsRegressor from sklearn.linear_mode…

Elasticsearch delete_by_query wrong usage

I am using 2 similar ES methods to load and delete documents:result = es.search(index=users_favourite_documents,doc_type=favourite_document,body={"query": {"match": {user: user}}})A…

SQLAlchemy: Lost connection to MySQL server during query

There are a couple of related questions regarding this, but in my case, all those solutions is not working out. Thats why I thought of asking again. I am getting this error while I am firing below quer…

row to columns while keeping part of dataframe, display on same row

I am trying to move some of my rows and make the them columns, but keep a large portion of the dataframe the same.Resulting Dataframe:ID Thing Level1 Level2 Time OAttribute IsTrue Score Value 1 …

SQLAlchemy InvalidRequestError when using composite foreign keys

My table relationships in SQLAlchemy have gotten quite complex, and now Im stuck at this error no matter how I configure my relationship.Im a bit new to SQLAlchemy so Im not sure what Im doing wrong, b…

google search by google api in r or python

I want to search some thing (ex:"python language") in google by python or R and it will give me the list of links for that google search like:https://en.wikipedia.org/wiki/Python_(programming…