2014-03-19 86 views
0

我想參考def check(self)中的標籤。它應該檢查來自外部設備的反饋是否等於某種東西,但是我不知道如何引用標籤並在相同或不相等的情況下更改標籤的顏色。我想要將例如lab3背景更改爲綠色或紅色,具體取決於平等。這裏是我的代碼:參考標籤Python,Tkinter

# -*- 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'。任何想法?這對我真的很重要,請幫助:)

回答

3

那是因爲你的標籤在def initUI(self):的範圍僅限定,因此無法進入def check(self)

嘗試定義你的按鈕爲self.lab1,而不是lab1,也指他們一樣的方法。

+0

非常感謝:)我會稍後再試 – Maq92

+0

@ Maq92,當然。沒問題。 – sshashank124