全局 tkinter 输入框没有属性 'get'

Global tkinter entry box has no attribute 'get'

首先让我说我是 Linux 的新手。我正在修改 github 中的一些代码。原始程序运行良好(带状图)。当我尝试从输入框中输入变量时,我收到以下错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python3.2/tkinter/__init__.py", line 1426, in __call__
    return self.func(*args)
  File "/home/pi/SCRAP.py", line 49, in show_entry_fields
    frequency = float (e1.get())
AttributeError: 'NoneType' object has no attribute 'get'

我添加的代码是在两个编辑开始和编辑结束注释之间。任何帮助是极大的赞赏。我的代码如下:

from tkinter import *
import math, random, threading, time

class StripChart:

    def __init__(self, root):
        self.gf = self.makeGraph(root)
        self.cf = self.makeControls(root)
        self.gf.pack()
        self.cf.pack()
        self.Reset()

    def makeGraph(self, frame):
        self.sw = 1000
        self.h = 200
        self.top = 2
        gf = Canvas(frame, width=self.sw, height=self.h+10,
                    bg="#002", bd=0, highlightthickness=0)
        gf.p = PhotoImage(width=2*self.sw, height=self.h)
        self.item = gf.create_image(0, self.top, image=gf.p, anchor=NW)
        return(gf)

    def makeControls(self, frame):
        cf = Frame(frame, borderwidth=1, relief="raised")
        Button(cf, text="Run", command=self.Run).grid(column=2, row=4)
        Button(cf, text="Stop", command=self.Stop).grid(column=4, row=4)
        Button(cf, text="Reset", command=self.Reset).grid(column=6, row=4)

#editing start
        Button(cf, text="Cycle", command=self.show_entry_fields).grid(column=7, row=4)
        Label(cf, text="Frequency(Hz)").grid(column=1, row=2)
        Label(cf, text="P-P Current(mA)").grid(column=1, row=3)

        global e1,e2
        e1=Entry(cf).grid(column=2, row=2)
        e2=Entry(cf).grid(column=2, row=3)
#editing end  

        self.fps = Label(cf, text="0 fps")
        self.fps.grid(column=2, row=5, columnspan=5)
        return(cf)

#editing start
    def show_entry_fields(self):
        #print("Frequency: %s\nMilliamps: %s\n" % (e1.get(),e2.get()))
        frequency = float (e1.get())
        currrent = float (e2.get())
        #print(check_var.get())
        print(frequency+1)
        print(current+1)
#editing end

    def Run(self):
        self.go = 1
        for t in threading.enumerate():
            if t.name == "_gen_":
                print("already running")
                return
        threading.Thread(target=self.do_start, name="_gen_").start()

    def Stop(self):
        self.go = 0
        for t in threading.enumerate():
            if t.name == "_gen_":
                t.join()

    def Reset(self):
        self.Stop()
        self.clearstrip(self.gf.p, '#345')

    def do_start(self):
        t = 0
        y2 = 0
        tx = time.time()
        while self.go:
            y1 = 0.2*math.sin(0.02*math.pi*t)
            y2 = 0.9*y2 + 0.1*(random.random()-0.5)
            self.scrollstrip(self.gf.p,
               (0.25+y1,   0.25, 0.7+y2,   0.6,     0.7,   0.8),
               ( '#ff4', '#f40', '#4af', '#080', '#0f0', '#080'),
                 "" if t % 65 else "#088")

            t += 1
            if not t % 100:
                tx2 = time.time()
                self.fps.config(text='%d fps' % int(100/(tx2 - tx)))
                tx = tx2
#            time.sleep(0.001)

    def clearstrip(self, p, color):  # Fill strip with background color
        self.bg = color              # save background color for scroll
        self.data = None             # clear previous data
        self.x = 0
        p.tk.call(p, 'put', color, '-to', 0, 0, p['width'], p['height'])

    def scrollstrip(self, p, data, colors, bar=""):   # Scroll the strip, add new data
        self.x = (self.x + 1) % self.sw               # x = double buffer position
        bg = bar if bar else self.bg
        p.tk.call(p, 'put', bg, '-to', self.x, 0,
                  self.x+1, self.h)
        p.tk.call(p, 'put', bg, '-to', self.x+self.sw, 0,
                  self.x+self.sw+1, self.h)
        self.gf.coords(self.item, -1-self.x, self.top)  # scroll to just-written column
        if not self.data:
            self.data = data
        for d in range(len(data)):
            y0 = int((self.h-1) * (1.0-self.data[d]))   # plot all the data points
            y1 = int((self.h-1) * (1.0-data[d]))
            ya, yb = sorted((y0, y1))
            for y in range(ya, yb+1):                   # connect the dots
                p.put(colors[d], (self.x,y))
                p.put(colors[d], (self.x+self.sw,y))
        self.data = data            # save for next call

def main():
    root = Tk()
    root.title("StripChart")
    app = StripChart(root)
    root.mainloop()

main()

您在定义 e1 时调用了 grid()。由于您这样做了,e1 被设置为 grid() 返回的内容,即 None:

>>> e1=Entry(cf).grid(column=2, row=2)

>>> e1
None

相反,创建小部件然后将其网格化在单独的行上:

>>> e1=Entry(cf)
>>> e1.grid(column=2, row=2)

>>> e1
<Tkinter.Entry instance at 0x01D43BC0>