⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stripchart.py

📁 Python.Tkinter编程实例代码多多学习
💻 PY
📖 第 1 页 / 共 2 页
字号:
from Tkinter import *
from ftplib import *
import Pmw
import string, time, os
import thread

from testdata import testData

####################################################################
# Constants
####################################################################

TEMP  = 1
PRESS = 2
SPEED = 3
HUM   = 4
VISIB = 5
DIR   = 6
CLOW  = 7
CHGH  = 8

BLANK_VALUE = -123321

noaa_url  = "weather.noaa.gov"
metar_dir = "/data/observations/metar/stations/"
temp_dir  = "/Temp"

digits = '0123456789'

####################################################################
# Weather Monitor
####################################################################

class WeatherMonitor:
    def __init__(self, master=None, host='', width=900, height=600,
                 bd=1, indent=30, titlespace=20):

	self.cells = [
          (0,0,1, 'Temp', -10, 110, TEMP, ['Temp'], 'Degrees F',
           [-10,0,10,20,30,40,50,60,70,80,90,100]),
          (1,0,1, 'Pressure', 27, 32, PRESS, ['Press'],'Inches Hg.',
           [27,28,29,30,31,32]),
          (2,0,0.4, 'Wind Speed', 0, 100, SPEED, ['WSpeed'], 'MPH',
           [0,20,40,60,80,100]),
          (2,0.48,0.40,'Wind Direction', 0, 360, DIR, ['WDir'], 'Degrees',
           [0,90,180,270,360]),
          (0,1,1, 'Humidity', 0, 100, HUM, ['Humidity'],  'Percent',
           [0,20,40,60,80,100]), 
          (1,1,1, 'Visibility', 0, 50, VISIB,['Visibility'], 'Miles',
           [0,10,20,30,40,50]), 
          (2,1,0.40, 'Cloud High', 10, 100, CHGH, ['CHigh', 'CVHigh'],
              'Feet (K)',
           [10,25,50,75,100]),
          (2,1.48,0.40, 'Cloud Low', 0, 30, CLOW, ['CLow'], 'Feet (K)',
           [0,5,10,15,20,25])
          ]

	self.XAxisData = {
            '5 Minutes':     ([0,1,2,3,4,5], 'Min'), 
            '10 Minutes':    ([0,2,4,6,8,10], 'Min'),
            '30 Minutes':    ([0,5,10,15,20,25,30], 'Min'), 
            '1 Hour':        ([0,10,20,30,40,50,60], 'Min'),
            '12 Hours':      ([0,2,4,6,8,10,12], 'Hrs'),
            '24 Hours':      ([0,4,8,12,16,20,24], 'Hrs')
                         }

	self.usePoll        = []
	self.Sessions       = {}
	self.wStation       = 'KPVD'

	self.Surround       = 'Gray10'
	self.Base           = 'Gray80'
        self.Graph          = 'Gray20'
	self.Line           = '#22ffdc'
	self.Axes           = 'MediumSeaGreen'
	self.Labels         = 'MediumSeaGreen'
	self.Titles         = 'DarkGoldenRod1'
	self.CrossHairsH    = '#006600'
	self.CrossHairsV    = '#008800'

	self.master        = master
	self.indent        = indent
	self.titlespace    = titlespace
	self.xindent       = (indent/4)*3
	self.yindent       = indent/4
	self.width         = width+(3*indent)
	self.height        = height+(2*indent)+(2*titlespace)
	self.cellWidth     = self.width/3
	self.cellHeight    = self.height/2
	self.axisWidth     = self.cellWidth - self.indent
	self.axisHeight    = self.cellHeight - \
                             (self.indent+self.titlespace)
	self.displayItems  = '12 Hours'
	self.refreshRate   = 60
	self.plottedData   = 'PMMarker'

        self.gotData       = FALSE
        self.threadRunning = FALSE
        self.testIndex     = 0

	self.frame=Frame(master, height=self.height,
                         width=self.width, relief='raised',
                         bd=bd)
	self.frame.pack(fill=BOTH, expand=0)
	self.innerframe=Frame(master, height=40, width=self.width,
                              relief='raised',
                              bg=self.Base, bd=bd)
	self.innerframe.pack(side=BOTTOM, fill=X, expand=0)
	self.refresh=Pmw.ComboBox(self.innerframe,
             history=0,
             entry_state = 'disabled', labelpos = W,
             label_text = 'Refresh Rate:',
             selectioncommand = self.setDisplayRate,
	     scrolledlist_items=('Default','1 Minute','5 Minutes',
                                 '10 Minutes','30 Minutes'))
	self.refresh.pack(padx=3, pady=3, side=LEFT)
	self.refresh.selectitem('Default')
	self.selector=Pmw.ComboBox(self.innerframe,
	     entry_state = 'disabled', history=0,
	     label_text = 'Display:', labelpos = W,
             selectioncommand = self.setDisplayRange,
	     scrolledlist_items=('10 Minutes','30 Minutes','1 Hour',
                                 '12 Hours','24 Hours'))
	self.selector.pack(padx=3, pady=3, side=LEFT)
	self.selector.selectitem('12 Hours')
	self.station=Pmw.ComboBox(self.innerframe,
             entry_state = 'disabled',
             history=0, label_text = 'Station:', labelpos = W,
             selectioncommand = self.setStation)
	self.station.pack(padx=3, pady=3, side=LEFT)

        fd = open('stations.txt')
        dataIn = fd.readlines()
        fd.close()
        stationList = []
        for data in dataIn:
            stationList.append(data[:-1])   # remove trailing newline
        self.station.setlist(stationList)
        self.station.selectitem('KPVD Providence')

	self.canvas=Canvas(self.frame, width=self.width,
             highlightthickness=0, height=self.height, bg=self.Surround)
	self.canvas.pack(side="top", fill='x', expand='no')

	self.canvas.create_line(0,self.cellHeight,self.width+1,
                                self.cellHeight,
				fill='gray40', width=2)
	self.canvas.create_line(self.cellWidth+1,0,self.cellWidth+1,
                                self.height+1,
				fill='gray40', width=2)
	self.canvas.create_line((2*self.cellWidth)+1,0,
                                (2*self.cellWidth)+1,self.height+1,
				fill='gray40', width=2)

	self.maxSpan          = None

	self.pollTime         = None  # Time for these data

        self.Temp             = None
        self.Press            = None
        self.WSpeed           = None
        self.Humidity         = None
        self.Visibility       = None
        self.WDir             = None
        self.CLow             = None
        self.CHigh            = None        
        self.CVHigh           = None        
        
        self.lastTemp         = (-999.0,-999.0)
        self.lastPress        = (-999.0,-999.0)
        self.lastWSpeed       = (-999.0,-999.0)
        self.lastHumidity     = (-999.0,-999.0)
        self.lastVisibility   = (-999.0,-999.0)
        self.lastWDir         = (-999.0,-999.0)
        self.lastCLow         = (-999.0,-999.0)
        self.lastCHigh        = (-999.0,-999.0)        
        self.lastCVHigh       = (-999.0,-999.0)        

        self.setup_plot()

    def setup_plot(self, restart=1):
	for xf, yf, af, label, low, high, dataType, datalist, \
                yAxisLabel, ticklist in self.cells:
	    x0=(self.cellWidth*xf)+self.xindent
	    x1=(self.cellWidth*xf)+self.xindent+self.axisWidth
	    y0=(self.cellHeight*yf)+self.yindent+self.titlespace+1
	    y1=(self.cellHeight*yf)+self.yindent+self.titlespace+\
                (self.axisHeight*af)+1
	    self.canvas.create_rectangle(x0,y0,x1,y1,
					 fill=self.Graph, outline="")
	    self.canvas.create_line(x0,y0,x0,y1,
				    fill=self.Axes, width=2)
            if low <= 0:
                spread=abs(low)+abs(high)
                yx=float((spread-abs(low)))/float(spread)*\
                    (self.axisHeight*af)+y0
                self.canvas.create_line(x0,yx,x1,yx,
                                        fill=self.Axes, width=2)

	    self.canvas.create_text(x1-2,y0-self.titlespace+2,
                               text=label, font=('Verdana', 12),
                               fill=self.Titles,anchor='ne')
	    self.canvas.create_text(x0+2,y0-self.titlespace+2,
                               text=yAxisLabel, font=('Verdana', 12),
                               fill=self.Titles,anchor='nw')

	    self.doYAxis(x0, x1, y0, af, low, high, ticklist)
	    self.doXAxis(x0, x1, y0, y1, label)
	    self.canvas.create_line(x0,y0,x1,y0,
				    fill=self.Axes, width=2)
	    self.canvas.create_line(x1,y0,x1,y1,
				    fill=self.Axes, width=2)
	    self.canvas.create_line(x0,y1,x1,y1,
				    fill=self.Axes, width=2)

	self.baseTime         = time.time()
	self.setDisplayRange(self.displayItems)

	if restart:
	    self.doPlot()

    def doYAxis(self, x0, x1, y0, af, low, high, ticklist):
        if low <= 0:
            spread=abs(low)+abs(high)
        else:
            spread = high-low
	negcomp=self.calculate_negative_component(low, high)
	for tick in ticklist:
            tickV = tick
            tickL = `tick`
	    y=((float(spread-(tickV+negcomp))/float(spread))*\
               (self.axisHeight*af))+y0-1
	    self.canvas.create_text(x0-2,y, text=tickL,
                                    font=('Verdana', 8),
                                    fill=self.Axes, anchor='e')
	    if not tickV == 0:
	        self.canvas.create_line(x0,y,x0+4,y, fill=self.Axes,
                                        width=2)
		self.canvas.create_line(x0+4,y,x1,y,
                                       fill=self.CrossHairsH, width=1)

    def doXAxis(self, x0, x1, y0, y1, tag):
	intlist, label = self.XAxisData[self.displayItems]
	ltag = translate_spaces(tag,'_')
	self.canvas.delete(ltag)   # Remove previous scales
	incr = self.axisWidth / (len(intlist)-1)
	xt = x0
	for tick in intlist:
	    if xt > x0:
	        self.canvas.create_line(xt,y1,xt,y1-4,
                                        fill=self.Axes, width=2,
                                        tags=ltag)
		self.canvas.create_line(xt,y0,xt,y1-4,
                                        fill=self.CrossHairsV,
                                        width=1, tags=ltag)
	    self.canvas.create_text(xt,y1+5, text=tick,
                                    font=('Verdana', 8),
                                    fill=self.Axes, anchor='n',
                                    tags=ltag)
	    xt = xt + incr
	self.canvas.create_text(xt-(incr*2)+(incr/2),y1+2,
                                text=label, font=('Verdana', 12),
                                fill=self.Titles, anchor='n',
                                tags=ltag)

    def doPlot(self):
        if self.gotData:
            self.gotData       = FALSE
            self.threadRunning = FALSE
        else:
            if self.threadRunning:
                self.frame.after(1000, self.doPlot)   # Spin for changes

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -