0

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 stock
    global d
    stock = stock_Entry.get()
    Quantity = Quantity_Entry.get()
    if stock not in d:
        d[stock] = Quantity
    else:
        d[stock] += Quantity




root = 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()
Jacob Vlijm
  • 3,099
  • 1
  • 21
  • 37
  • 3
    Hi user3687942, posted. Please mention if you manage. I noticed you *never* accepted an answer before. That *could* be because you never got a satisfying answer, but also because you are not familiar with the fact that accepting is the appropriate way to indicate an answer works for you. *IF* the answer works for you, please consider accepting. (tick the big "V" on the left. – Jacob Vlijm Sep 02 '16 at 10:42
  • @Tiger-222 nope, different issue. – Jacob Vlijm Sep 02 '16 at 10:45

1 Answers1

2

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

Jacob Vlijm
  • 3,099
  • 1
  • 21
  • 37
  • One *can* create and place/pack/grid a widget in one statement. I do it when I will not need to access the widget again. (But one should consider test needs.) One just needs to know that the return is then `None` and that it will be very hard to to ever get a handle on widget again. You are correct that beginners should not take the shortcut until they understand what they are doing. – Terry Jan Reedy Sep 02 '16 at 20:55
  • @TerryJanReedy Thanks once again! edited. – Jacob Vlijm Sep 02 '16 at 21:15