Python tkinter 的 entry.get() 不起作用,我该如何解决? [英] Python tkinter's entry.get() does not work, how can I fix it?

查看:67
本文介绍了Python tkinter 的 entry.get() 不起作用,我该如何解决?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为大学构建一个简单的程序.我们必须将代码转换为接口.我设法制作了界面,但我似乎无法将我的值从 Entry 传递到实际代码.这是我的代码:

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()

推荐答案

如果您稍后仍需要访问它,则不能像这样同时创建小部件和网格.stock_Entry.get() 将引发 AttributeError (NoneType).

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).

代替:

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

使用:

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

Quantity_Entry 相同:

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

...它的工作原理:

...and it works:

这篇关于Python tkinter 的 entry.get() 不起作用,我该如何解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆