全局名称未定义

时间:2010-06-10 04:09:48

标签: python global-variables tkinter

我用Python编写了一个CPU监控程序。出于某种原因,有时程序将运行没有任何问题。然后其他时间由于以下错误,程序甚至不会启动。

Traceback (most recent call last):

文件“”,第244行,在run_nodebug中   文件“C:\ Python26 \ CPUR1.7.pyw”,第601行,in     app = simpleapp_tk(无)   文件“C:\ Python26 \ CPUR1.7.pyw”,第26行,在 init 中     self.initialize()   初始化时,文件“C:\ Python26 \ CPUR1.7.pyw”,第107行     self.F()   文件“C:\ Python26 \ CPUR1.7.pyw”,第517行,F     S2 = TL.entryVariableS.get() NameError:未定义全局名称“TL”

我似乎无法找到问题,也许更有经验的人可以帮助我?

以下是给我带来麻烦的部分片段: (片段中倒数第二行是给我带来麻烦的)

def E(self):


    if self.selectedM.get() =='Options...':

设置

        global TL
        TL = Tkinter.Toplevel(self)




        menu = Tkinter.Menu(TL)
        TL.config(menu=menu)

        filemenu = Tkinter.Menu(menu)
        menu.add_cascade(label="|     Menu     |", menu=filemenu)

        filemenu.add_command(label="Instruction Manual...", command=self.helpmenu)
        filemenu.add_command(label="About...", command=self.aboutmenu)
        filemenu.add_separator()
        filemenu.add_command(label="Exit Options", command=TL.destroy)
        filemenu.add_command(label="Exit", command=self.destroy)

        helpmenu = Tkinter.Menu(menu)
        menu.add_cascade(label="|     Help     |", menu=helpmenu)
        helpmenu.add_command(label="Instruction Manual...", command=self.helpmenu)
        helpmenu.add_separator()
        helpmenu.add_command(label="Quick Help...", command=self.helpmenu)

标题

        TL.label5 = Tkinter.Label(TL,text="CPU Usage: Options",anchor="center",fg="black",bg="lightgreen",relief="ridge",borderwidth=5,font=('Arial', 18, 'bold'))
        TL.label5.pack(padx=15,ipadx=5)

X Y比例

        TL.separator = Tkinter.Frame(TL,height=7, bd=1, relief='ridge', bg='grey95')
        TL.separator.pack(pady=5,padx=5)
        TL.sclX = Tkinter.Scale(TL.separator, from_=0, to=1500, orient='horizontal', resolution=1, command=self.A)
        TL.sclX.grid(column=1,row=0,ipadx=27, sticky='w')

        TL.label1 = Tkinter.Label(TL.separator,text="X",anchor="s",fg="black",bg="grey95",font=('Arial', 8 ,'bold'))
        TL.label1.grid(column=0,row=0, pady=1, sticky='S')





        TL.sclY = Tkinter.Scale(TL.separator, from_=0, to=1500, resolution=1, command=self.A)
        TL.sclY.grid(column=2,row=1,rowspan=2,sticky='e', padx=4)

        TL.label3 = Tkinter.Label(TL.separator,text="Y",fg="black",bg="grey95",font=('Arial', 8 ,'bold'))
        TL.label3.grid(column=2,row=0, padx=10, sticky='e')





        TL.entryVariable2 = Tkinter.StringVar()
        TL.entry2 = Tkinter.Entry(TL.separator,textvariable=TL.entryVariable2,
                                    fg="grey15",bg="grey90",relief="sunken",insertbackground="black",borderwidth=5,font=('Arial', 10))
        TL.entry2.grid(column=1,row=1,ipadx=20, pady=10,sticky='EW')
        TL.entry2.bind("<Return>", self.B)

        TL.label2 = Tkinter.Label(TL.separator,text="X:",fg="black",bg="grey95",font=('Arial', 8 ,'bold'))
        TL.label2.grid(column=0,row=1, ipadx=4, sticky='W')





        TL.entryVariable1 = Tkinter.StringVar()
        TL.entry1 = Tkinter.Entry(TL.separator,textvariable=TL.entryVariable1,
                                    fg="grey15",bg="grey90",relief="sunken",insertbackground="black",borderwidth=5,font=('Arial', 10))
        TL.entry1.grid(column=1,row=2,sticky='EW')
        TL.entry1.bind("<Return>", self.B)


        TL.label4 = Tkinter.Label(TL.separator,text="Y:", anchor="center",fg="black",bg="grey95",font=('Arial', 8 ,'bold'))
        TL.label4.grid(column=0,row=2, ipadx=4, sticky='W')



        TL.label7 = Tkinter.Label(TL.separator,text="Text Colour:",fg="black",bg="grey95",font=('Arial', 8 ,'bold'),justify='left')
        TL.label7.grid(column=1,row=3,  sticky='W',padx=10,ipady=10,ipadx=30)

        TL.selectedP = Tkinter.StringVar()
        TL.opt1 = Tkinter.OptionMenu(TL.separator, TL.selectedP,'Normal', 'White','Black', 'Blue', 'Steel Blue','Green','Light Green','Yellow','Orange' ,'Red',command=self.G)

        TL.opt1.config(fg="black",bg="grey90",activebackground="grey90",activeforeground="black",
                     anchor="center",relief="raised",direction='right',font=('Arial', 10))
        TL.opt1.grid(column=1,row=4,sticky='EW',padx=20,ipadx=20)
        TL.selectedP.set('Normal')



        TL.sclS = Tkinter.Scale(TL.separator, from_=10, to=2000, orient='horizontal', resolution=10, command=self.H)
        TL.sclS.grid(column=1,row=5,ipadx=27, sticky='w')
        TL.sclS.set(600)


        TL.entryVariableS = Tkinter.StringVar()
        TL.entryS = Tkinter.Entry(TL.separator,textvariable=TL.entryVariableS,
                                    fg="grey15",bg="grey90",relief="sunken",insertbackground="black",borderwidth=5,font=('Arial', 10))
        TL.entryS.grid(column=1,row=6,ipadx=20, pady=10,sticky='EW')
        TL.entryS.bind("<Return>", self.I)
        TL.entryVariableS.set(600)
        TL.resizable(False,False)
        TL.title('Options')

        geomPatt = re.compile(r"(\d+)?x?(\d+)?([+-])(\d+)([+-])(\d+)")
        s = self.wm_geometry()
        m = geomPatt.search(s)
        X = m.group(4)
        Y = m.group(6)

        TL.sclY.set(Y)
        TL.sclX.set(X)





    if self.selectedM.get() == 'Exit':

        self.destroy()


def F (self):


    G = round(psutil.cpu_percent(), 1)
    G1 = str(G) + '%'



    self.labelVariable.set(G1)


    if G < 5:
        self.imageLabel.configure(image=self.image0)
    if G >= 5:
        self.imageLabel.configure(image=self.image5)
    if G >= 10:
        self.imageLabel.configure(image=self.image10)
    if G >= 15:
        self.imageLabel.configure(image=self.image15)

    if G >= 20:
        self.imageLabel.configure(image=self.image20)

    if G >= 25:
        self.imageLabel.configure(image=self.image25)



    if G >= 30:
        self.imageLabel.configure(image=self.image30)

    if G >= 35:
        self.imageLabel.configure(image=self.image35)

    if G >= 40:
        self.imageLabel.configure(image=self.image40)

    if G >= 45:
        self.imageLabel.configure(image=self.image45)


    if G >= 50:
        self.imageLabel.configure(image=self.image50)

    if G >= 55:
        self.imageLabel.configure(image=self.image55)

    if G >= 60:
        self.imageLabel.configure(image=self.image60)

    if G >= 65:
        self.imageLabel.configure(image=self.image65)




    if G >= 70:
        self.imageLabel.configure(image=self.image70)

    if G >= 75:
        self.imageLabel.configure(image=self.image75)

    if G >= 80:
        self.imageLabel.configure(image=self.image80)

    if G >= 85:
        self.imageLabel.configure(image=self.image85)



    if G >= 90:
        self.imageLabel.configure(image=self.image90)

    if 100> G >= 95:
        self.imageLabel.configure(image=self.image95)

    if G == 100:
        self.imageLabel.configure(image=self.image100)

    S2 = TL.entryVariableS.get()



    self.after(int(S2), self.F)

1 个答案:

答案 0 :(得分:2)

编辑:

如果if self.selectedM.get() =='Options...':中的E()语句不满足,则永远不会声明全局变量TL,我非常确定发生了什么。然后,当F()尝试使用TL时,它就不存在。

相关问题