参考Labels Python,Tkinter

时间:2014-03-19 09:17:52

标签: python tkinter

我想在def check(self)中引用标签。它应检查来自外部设备的反馈是否等于某些东西,但我不知道如何引用标签并改变它的颜色,如果它相等或不相同。我希望将lab3背景更改为绿色或红色,具体取决于相等性。这是我的代码:

# -*- coding: utf-8 -*-

import Tkinter as T, tkFileDialog
import os
from time import *
import serial
from ttk import Button, Label, Frame, Entry, Style

class Program(Frame):

    def __init__(self, root):

        Frame.__init__(self, root)
        self.root = root       
        self.initUI()

    def initUI(self):

        self.root.title('OptoMaQ')
        Style().configure('TFrame', background = '#6666ff')
        Style().configure('TButton',background = '#6666ff')

        lab1 = Label(self, text = 'Press Save to save a file', background = '#6666ff').grid(row = 0, columnspan = 5)
        but1 = Button(self, text='Save', command=self.save).grid(row = 2,column = 1)
        lab2 = Label(self, text = 'Press Exit to quite', background = '#6666ff').grid(row = 1, columnspan = 5)
        but2 = Button(self, text = 'Exit',command = self.exit).grid(row = 2, column = 2)              
        lab3 = Label(self, text = 'Spectra-Hub', background = '#6666ff').grid(row = 3, columnspan = 5)         
        lab4 = Label(self, text = 'SpectraPro VM-504',background = '#6666ff').grid(row = 4,columnspan = 5)
        lab5 = Label(self, text = 'SpectraPro SP-2-300i',background = '#6666ff').grid(row = 5, columnspan = 5)
        but3 = Button(self, text = 'Check',command = self.check).grid(row = 6, columnspan = 5)
        lab6 = Label(self, text = 'Type sth here', background = '#6666ff').grid(row = 7,columnspan = 5)
        self.entry = Entry(self, justify = 'center',text = '1')
        self.entry.grid(row = 8, columnspan =3)

        self.fileop = options = {}
        options['filetypes'] = [('all files', '.*'),('dat files','.dat'),('text files', '.txt')]
        options['initialfile'] = 'file.txt'
        options['parent'] = root

    def check(self):

        port = serial.Serial(15)
        command = 'WHO'
        port.write(command + '\r')
        out = port.read(50)
        if out == 'Acton Research Corp. \nSpectra-Hub':
            lab3 = Label(self, text = 'Spectra-Hub', background = '#6666ff').grid(row = 3, columnspan = 5)
            lab3.config(background = 'green')
            port.close()
        else:
            lab3 = Label(self, text = 'Spectra-Hub', background = '#6666ff').grid(row = 3, columnspan = 5)
            lab3.config(background = 'red')
            port.close()

        port2 = serial.Serial(16)
        port2.write(command +'\r')
        out2 = port2.read(50)
        if out2 == 'Acton Research Corp. \nSpectraPro VM-504':

            port2.close()
        else:

            port2.close()

        port3 = serial.Serial(17)
        port3.write(command + '\r')
        out3 = port3.read(46)
        if out3 == 'Acton Research Corp. \n SpectraPro SP-2-300i':

            port3.close()
        else:
            port3.close()
    def save(self):
        filename = tkFileDialog.asksaveasfilename(**self.fileop)

        if filename:
            file = open(filename, 'a+')
            time = strftime("%A, %d %b %Y, %H:%M:%S ", gmtime())
            print time
            file.write(time)
            file.write('\n')
            input = str(self.entry.get())
            file.write(input)
            file.close()

    def exit(self):
        root.destroy()


if __name__=='__main__':
    root = T.Tk()
    Program(root).pack()
    root.mainloop()

我已经在第46-53行尝试了类似的东西,但它不起作用。它显示第52行'NoneType' object has no attribute 'config'。任何想法?这对我来说非常重要,请帮助:)

1 个答案:

答案 0 :(得分:3)

这是因为您的标签仅在def initUI(self):范围内定义,因此无法访问def check(self)

尝试将按钮定义为self.lab1而不是lab1,并以同样的方式引用它们。