关于标签Python,Tkin

2024-10-02 22:24:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我想参考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'。有什么想法吗?这对我来说真的很重要,请帮帮我:)


Tags: textimportselfclosedefrootlabelcommand