📄 components_4.py
字号:
# Components_4.py for chapter 10 examples
from Tkinter import *
from GUICommon import *
from Common import *
st_wid = []
ops = ['turnon','turnoff','warn','alarm','blinkon','blinkoff']
glb = DummyClass()
class LED(GUICommon):
def __init__(self, master=None, width=25, height=25,
appearance=FLAT,
status=STATUS_ON, bd=1,
bg=None,
shape=SQUARE, outline="",
blink=0, blinkrate=1,
orient=POINT_UP,
takefocus=0):
# preserve attributes
self.master = master
self.shape = shape
self.Colors = [None, Color.OFF, Color.ON,
Color.WARN, Color.ALARM, '#00ffdd']
self.status = status
self.blink = blink
self.blinkrate = int(blinkrate)
self.on = 0
self.onState = None
st_wid.append(self) # register for animation
if not bg:
bg = Color.PANEL
## Base frame to contain light
self.led_frame=Frame(master, relief=appearance, bg=bg, bd=bd,
takefocus=takefocus)
basesize = width
d = center = int(basesize/2)
if self.shape == SQUARE:
self.canvas=Canvas(self.led_frame, height=height, width=width,
bg=bg, bd=0, highlightthickness=0)
self.light=self.canvas.create_rectangle(0, 0, width, height,
fill=Color.ON)
elif self.shape == ROUND:
r = int((basesize-2)/2)
self.canvas=Canvas(self.led_frame, width=width, height=width,
highlightthickness=0, bg=bg, bd=0)
if bd > 0:
self.border=self.canvas.create_oval(center-r, center-r,
center+r, center+r)
r = r - bd
self.light=self.canvas.create_oval(center-r-1, center-r-1,
center+r, center+r,
fill=Color.ON,
outline=outline)
else: # Default is an ARROW
self.canvas=Canvas(self.led_frame, width=width, height=width,
highlightthickness=0, bg=bg, bd=0)
x = d
y = d
VL = ARROW_HEAD_VERTICES[orient]
self.light=self.canvas.create_polygon(eval(VL[0]),
eval(VL[1]), eval(VL[2]), eval(VL[3]),
eval(VL[4]), eval(VL[5]), eval(VL[6]),
eval(VL[7]), outline = outline)
self.canvas.pack(side=TOP, fill=X, expand=NO)
self.update()
def update(self):
# First do the blink, if set to blink
if self.blink:
if self.on:
if not self.onState:
self.onState = self.status
self.status = STATUS_OFF
self.on = 0
else:
if self.onState:
self.status = self.onState # Current ON color
self.on = 1
# Set color for current status
self.canvas.itemconfig(self.light, fill=self.Colors[self.status])
self.canvas.update_idletasks()
if self.blink:
self.led_frame.after(self.blinkrate * 1000, self.update)
class CLED(GUICommon):
def __init__(self, frame=None, ocanv=None, width=25, height=25,
status=STATUS_ON, bd=0,
bg=None,
shape=SQUARE, outline="",
blink=0, blinkrate=1,
orient=POINT_UP,
takefocus=0,
relx=0, rely=0):
# preserve attributes
self.frame = frame
self.ocanv = ocanv
self.shape = shape
self.Colors = [None, Color.OFF, Color.ON,
Color.WARN, Color.ALARM, '#00ffdd']
self.status = status
self.blink = blink
self.blinkrate = int(blinkrate)
self.on = 0
self.onState = None
st_wid.append(self) # register for animation
basesize = width
d = center = int(basesize/2)
if self.shape == SQUARE:
self.light=self.ocanv.create_rectangle(relx, rely,
relx+width, rely+height,
fill=Color.ON)
else: # Default is an round
r = int((basesize-2)/2)
if bd > 0:
self.border=self.ocanv.create_oval(relx-r, rely-r,
rely+r, rely+r)
r = r - bd
self.light=self.ocanv.create_oval(relx-r-1, rely-r-1,
relx+r, rely+r,
fill=Color.ON,
outline=outline)
self.update()
def update(self):
# First do the blink, if set to blink
if self.blink:
if self.on:
if not self.onState:
self.onState = self.status
self.status = STATUS_OFF
self.on = 0
else:
if self.onState:
self.status = self.onState # Current ON color
self.on = 1
# Set color for current status
self.ocanv.itemconfig(self.light, fill=self.Colors[self.status])
self.frame.update_idletasks()
if self.blink:
self.frame.after(self.blinkrate * 1000, self.update)
class Screen(GUICommon):
def __init__(self, master, bg=Color.PANEL, height=1, width=1):
self.screen_frame = Frame(master, width=width, height=height,
bg=bg, bd=0)
self.base = bg
self.set_colors(self.screen_frame)
radius = 4 # radius of an air hole
ssize = radius*3 # spacing between holes
rows = int(height/ssize)
cols = int(width/ssize)
self.canvas = Canvas(self.screen_frame, height=height, width=width,
bg=bg, bd=0, highlightthickness=0)
self.canvas.pack(side=TOP, fill=BOTH, expand=NO)
y = ssize - radius
for r in range(rows):
x0 = ssize -radius
for c in range(cols):
x = x0 + (ssize*c)
self.canvas.create_oval(x-radius, y-radius,
x+radius, y+radius,
fill=self.dbase,
outline=self.lbase)
y = y + ssize
class PowerConnector:
def __init__(self, master, bg=Color.PANEL):
self.socket_frame = Frame(master, relief="raised", width=60,
height=40, bg=bg, bd=4)
inside=Frame(self.socket_frame, relief="sunken", width=56,
height=36, bg=Color.INSIDE, bd=2)
inside.place(relx=.5, rely=.5, anchor=CENTER)
ground=Frame(inside, relief="raised", width=6, height=10,
bg=Color.CHROME, bd=2)
ground.place(relx=.5, rely=.3, anchor=CENTER)
p1=Frame(inside, relief="raised", width=6, height=10,
bg=Color.CHROME, bd=2)
p1.place(relx=.25, rely=.7, anchor=CENTER)
p2=Frame(inside, relief="raised", width=6, height=10,
bg=Color.CHROME, bd=2)
p2.place(relx=.75, rely=.7, anchor=CENTER)
class PowerSwitch(GUICommon):
def __init__(self, master, label='I 0', base=Color.PANEL):
self.base = base
self.set_colors(master)
self.switch_frame = Frame(master, relief="raised", width=45,
height=28, bg=self.vlbase, bd=4)
switch = Frame(self.switch_frame, relief="sunken", width=32,
height=22, bg=self.base, bd=2)
switch.place(relx=0.5, rely=0.5, anchor=CENTER)
lbl=Label(switch, text=label, font=("Verdana", 10, "bold"),
fg='white', bd=0, bg=self.dbase)
lbl.place(relx=0.5, rely=0.5, anchor=CENTER)
class Enet10baseT(GUICommon):
def __init__(self, master, orient=HW_UP, status=STATUS_ON,
port=-1, fid='', xwidth=30, xheight=24):
self.hitID = fid
self.Colors = [None, Color.CHROME, Color.ON,
Color.WARN, Color.ALARM, '#00ffdd']
if orient == HW_UP or orient == HW_DOWN:
width = xwidth
height = xheight
iwidth = width*0.8
iheight = height - (width*0.2)
jwidth = width / 3
jheight = height / 4
else:
width = xheight
height = xwidth
iwidth = width - (height*0.2)
iheight = height*0.8
jwidth = height / 3
jheight = width / 4
self.status = status
self.blink = 0
self.blinkrate = 1
self.on = 0
self.onState = None
self.high = FALSE
st_wid.append(self) # register for animation
self.j45_frame = Frame(master, width=width, height=height, bd=2,
relief="raised", bg=Color.CHROME,
takefocus=1)
self.j45_frame.pack(fill=BOTH, expand=1)
self.j45_frame.bind('<FocusIn>', self.focus_in)
self.j45_frame.bind('<FocusOut>', self.focus_out)
self.i1 = Frame(self.j45_frame, relief="sunken", width=iwidth,
height=iheight, bg=Color.INSIDE, bd=1)
self.i1.place(relx=0.05, rely=0.04, anchor=NW)
self.i2 = Frame(self.j45_frame, relief="flat", width=jwidth,
height=jheight, bg=Color.INSIDE, bd=2)
xx, yy = [(0.5,0),(0.5,1),(0,0.5),(1,0.5)][orient]
self.i2.place(relx=xx, rely=yy, anchor=CENTER)
if self.hitID:
self.hitID = '%s.%d' % (self.hitID, port)
for widget in [self.j45_frame, self.i1, self.i2]:
widget.bind('<KeyPress-space>', self.panelMenu)
widget.bind('<Button-1>', self.panelMenu)
def focus_in(self, event):
self.last_background = self.j45_frame['background']
self.j45_frame['background'] = Color.HIGHLIGHT
self.high = TRUE
self.update()
def focus_out(self, event):
self.j45_frame['background'] = self.last_background
self.high = FALSE
self.update()
def update(self):
# First do the blink, if set to blink
if self.blink:
if self.on:
if not self.onState:
self.onState = self.status
self.status = STATUS_OFF
self.on = 0
else:
if self.onState:
self.status = self.onState # Current ON color
self.on = 1
# Set color for current status
if not self.high:
self.j45_frame['background'] = self.Colors[self.status]
self.j45_frame.update_idletasks()
if self.blink:
self.j45_frame.after(self.blinkrate * 1000, self.update)
class BNC(GUICommon):
def __init__(self, master, status=0, diameter=18, port=-1, fid=''):
self.base = master['background']
self.hitID = fid
self.status=status
self.blink = 0
self.blinkrate = 1
self.on = 0
self.onState = None
self.Colors = [None, Color.CHROME, Color.ON,
Color.WARN, Color.ALARM, '#00ffdd']
st_wid.append(self) # register for animation
basesize = diameter+6
self.bnc_frame = Frame(master, relief="flat", bg=self.base,
bd=0, highlightthickness=0, takefocus=1)
self.bnc_frame.pack(expand=0)
self.bnc_frame.bind('<FocusIn>', self.focus_in)
self.bnc_frame.bind('<FocusOut>', self.focus_out)
self.canvas=Canvas(self.bnc_frame, width=basesize, height=basesize,
highlightthickness=0, bg=self.base, bd=0)
center = basesize/2
r = diameter/2
self.pins=self.canvas.create_rectangle(0, center+2, basesize-1, 10,
fill=Color.CHROME)
self.bnc=self.canvas.create_oval(center-r, center-r,
center+r, center+r,
fill=Color.CHROME, outline="black")
r = r-3
self.canvas.create_oval(center-r, center-r, center+r, center+r,
fill=Color.INSIDE, outline='black')
r = r-2
self.canvas.create_oval(center-r, center-r, center+r, center+r,
fill=Color.CHROME)
r = r-3
self.canvas.create_oval(center-r, center-r, center+r, center+r,
fill=Color.INSIDE, outline='black')
self.canvas.pack(side=TOP, fill=X, expand=0)
if self.hitID:
self.hitID = '%s.%d' % (self.hitID, port)
for widget in [self.bnc_frame]:
widget.bind('<KeyPress-space>', self.panelMenu)
widget.bind('<Button-1>', self.panelMenu)
for widget in [self.canvas]:
widget.bind('<1>', self.panelMenu)
def focus_in(self, event):
self.last_background = self.canvas.itemcget(self.bnc, 'fill')
self.canvas.itemconfig(self.bnc, fill=Color.HIGHLIGHT)
self.update()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -