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

📄 widget.py

📁 Urwid is a Python library for making text console applications. It has many features including fluid
💻 PY
📖 第 1 页 / 共 5 页
字号:
		self._body = body		self._invalidate()	body = property(get_body, set_body)		def selectable(self):		"""Return selectable from body."""		return self._body.selectable()		def filler_values(self, (maxcol, maxrow), focus):		"""Return the number of rows to pad on the top and bottom.				Override this method to define custom padding behaviour."""		if self.height_type is None:			height = self.body.rows((maxcol,),focus=focus)			return calculate_filler( self.valign_type,				self.valign_amount, 'fixed', height, 				None, maxrow )					return calculate_filler( self.valign_type, self.valign_amount,			self.height_type, self.height_amount,			self.min_height, maxrow)		def render(self, (maxcol,maxrow), focus=False):		"""Render self.body with space above and/or below."""		top, bottom = self.filler_values((maxcol,maxrow), focus)				if self.height_type is None:			canv = self.body.render( (maxcol,), focus)		else:			canv = self.body.render( (maxcol,maxrow-top-bottom),focus)		canv = CompositeCanvas(canv)				if maxrow and canv.rows() > maxrow and canv.cursor is not None:			cx, cy = canv.cursor			if cy >= maxrow:				canv.trim(cy-maxrow+1,maxrow-top-bottom)		if canv.rows() > maxrow:			canv.trim(0, maxrow)			return canv		canv.pad_trim_top_bottom(top, bottom)		return canv	def keypress(self, (maxcol,maxrow), key):		"""Pass keypress to self.body."""		if self.height_type is None:			return self.body.keypress( (maxcol,), key )		top, bottom = self.filler_values((maxcol,maxrow), True)		return self.body.keypress( (maxcol,maxrow-top-bottom), key )	def get_cursor_coords(self, (maxcol,maxrow)):		"""Return cursor coords from self.body if any."""		if not hasattr(self.body, 'get_cursor_coords'):			return None					top, bottom = self.filler_values((maxcol,maxrow), True)		if self.height_type is None:			coords = self.body.get_cursor_coords((maxcol,))		else:			coords = self.body.get_cursor_coords(				(maxcol,maxrow-top-bottom))		if not coords:			return None		x, y = coords		if y >= maxrow:			y = maxrow-1		return x, y+top	def get_pref_col(self, (maxcol,maxrow)):		"""Return pref_col from self.body if any."""		if not hasattr(self.body, 'get_pref_col'):			return None				if self.height_type is None:			x = self.body.get_pref_col((maxcol,))		else:			top, bottom = self.filler_values((maxcol,maxrow), True)			x = self.body.get_pref_col(				(maxcol,maxrow-top-bottom))		return x		def move_cursor_to_coords(self, (maxcol,maxrow), col, row):		"""Pass to self.body."""		if not hasattr(self.body, 'move_cursor_to_coords'):			return True				top, bottom = self.filler_values((maxcol,maxrow), True)		if row < top or row >= maxcol-bottom:			return False		if self.height_type is None:			return self.body.move_cursor_to_coords((maxcol,),				col, row-top)		return self.body.move_cursor_to_coords(			(maxcol, maxrow-top-bottom), col, row-top)		def mouse_event(self, (maxcol,maxrow), event, button, col, row, focus):		"""Pass to self.body."""		if not hasattr(self.body, 'mouse_event'):			return False				top, bottom = self.filler_values((maxcol,maxrow), True)		if row < top or row >= maxcol-bottom:			return False		if self.height_type is None:			return self.body.mouse_event((maxcol,),				event, button, col, row-top, focus)		return self.body.mouse_event( (maxcol, maxrow-top-bottom), 			event, button,col, row-top, focus)				class OverlayError(Exception):	passclass Overlay(BoxWidget):	def __init__(self, top_w, bottom_w, align, width, valign, height,			min_width=None, min_height=None ):		"""		top_w -- a flow, box or fixed widget to overlay "on top"		bottom_w -- a box widget to appear "below" previous widget		align -- one of:		    'left', 'center', 'right'		    ('fixed left', columns)		    ('fixed right', columns)		    ('relative', percentage 0=left 100=right)		width -- one of:		    None if top_w is a fixed widget		    number of columns wide		    ('fixed right', columns)  Only if align is 'fixed left'		    ('fixed left', columns)  Only if align is 'fixed right'		    ('relative', percentage of total width)		valign -- one of:		    'top', 'middle', 'bottom'		    ('fixed top', rows)		    ('fixed bottom', rows)		    ('relative', percentage 0=top 100=bottom)		height -- one of:		    None if top_w is a flow or fixed widget		    number of rows high 		    ('fixed bottom', rows)  Only if valign is 'fixed top'		    ('fixed top', rows)  Only if valign is 'fixed bottom'		    ('relative', percentage of total height)		min_width -- the minimum number of columns for top_w		    when width is not fixed		min_height -- one of:		    minimum number of rows for the widget when height not fixed				Overlay widgets behave similarly to Padding and Filler widgets		when determining the size and position of top_w.  bottom_w is		always rendered the full size available "below" top_w.		"""		self.__super.__init__()		at,aa,wt,wa=decompose_align_width(align, width, OverlayError)		vt,va,ht,ha=decompose_valign_height(valign,height,OverlayError)				self.top_w = top_w		self.bottom_w = bottom_w				self.align_type, self.align_amount = at, aa		self.width_type, self.width_amount = wt, wa		if self.width_type and self.width_type != 'fixed':			self.min_width = min_width		else:			self.min_width = None				self.valign_type, self.valign_amount = vt, va		self.height_type, self.height_amount = ht, ha		if self.height_type not in ('fixed', None):			self.min_height = min_height		else:			self.min_height = None	def selectable(self):		"""Return selectable from top_w."""		return self.top_w.selectable()		def keypress(self, size, key):		"""Pass keypress to top_w."""		return self.top_w.keypress(self.top_w_size(size,                       *self.calculate_padding_filler(size, True)), key)		def get_cursor_coords(self, size):		"""Return cursor coords from top_w, if any."""		if not hasattr(self.body, 'get_cursor_coords'):			return None		left, right, top, bottom = self.calculate_padding_filler(size,			True)		x, y = self.top_w.get_cursor_coords(			(maxcol-left-right, maxrow-top-bottom) )		if y >= maxrow:  # required??			y = maxrow-1		return x+left, y+top		def calculate_padding_filler(self, (maxcol, maxrow), focus):		"""Return (padding left, right, filler top, bottom)."""		height = None		if self.width_type is None:			# top_w is a fixed widget			width, height = self.top_w.pack(focus=focus)			assert height, "fixed widget must have a height"			left, right = calculate_padding(self.align_type,				self.align_amount, 'fixed', width, 				None, maxcol, clip=True )		else:			left, right = calculate_padding(self.align_type,				self.align_amount, self.width_type,				self.width_amount, self.min_width, maxcol)		if height:			# top_w is a fixed widget			top, bottom = calculate_filler(self.valign_type, 				self.valign_amount, 'fixed', height,				None, maxrow)			if maxrow-top-bottom < height:				bottom = maxrow-top-height		elif self.height_type is None:			# top_w is a flow widget			height = self.body.rows((maxcol,),focus=focus)			top, bottom =  calculate_filler( self.valign_type,				self.valign_amount, 'fixed', height, 				None, maxrow )		else:				top, bottom = calculate_filler(self.valign_type, 				self.valign_amount, self.height_type, 				self.height_amount, self.min_height, maxrow)		return left, right, top, bottom		def top_w_size(self, size, left, right, top, bottom):		"""Return the size to pass to top_w."""		if self.width_type is None:			# top_w is a fixed widget			return ()		maxcol, maxrow = size		if self.width_type is not None and self.height_type is None:			# top_w is a flow widget			return (maxcol-left-right,)		return (maxcol-left-right, maxrow-top-bottom)					def render(self, size, focus=False):		"""Render top_w overlayed on bottom_w."""		left, right, top, bottom = self.calculate_padding_filler(size,			focus)		bottom_c = self.bottom_w.render(size)		top_c = self.top_w.render(			self.top_w_size(size, left, right, top, bottom), focus)		if left<0 or right<0:			top_c = CompositeCanvas(top_c)			top_c.pad_trim_left_right(min(0,left), min(0,right))		if top<0 or bottom<0:			top_c = CompositeCanvas(top_c)			top_c.pad_trim_top_bottom(min(0,top), min(0,bottom))				return CanvasOverlay(top_c, bottom_c, max(0,left), top)	def mouse_event(self, size, event, button, col, row, focus):		"""Pass event to top_w, ignore if outside of top_w."""		if not hasattr(self.top_w, 'mouse_event'):			return False				left, right, top, bottom = self.calculate_padding_filler(size,			focus)		maxcol, maxrow = size		if ( col<left or col>=maxcol-right or			row<top or row>=maxrow-bottom ):			return False					return self.top_w.mouse_event(			self.top_w_size(size, left, right, top, bottom),			event, button, col-left, row-top, focus )	def decompose_align_width( align, width, err ):	try:		if align in ('left','center','right'):			align = (align,0)		align_type, align_amount = align		assert align_type in ('left','center','right','fixed left',			'fixed right','relative')	except:		raise err("align value %s is not one of 'left', 'center', "			"'right', ('fixed left', columns), ('fixed right', "			"columns), ('relative', percentage 0=left 100=right)" 			% `align`)	try:		if width is None:			width = None, None		elif type(width) == type(0):			width = 'fixed', width		width_type, width_amount = width		assert width_type in ('fixed','fixed right','fixed left',			'relative', None)	except:		raise err("width value %s is not one of ('fixed', columns "			"width), ('fixed right', columns), ('relative', "			"percentage of total width), None" % `width`)			if width_type == 'fixed left' and align_type != 'fixed right':		raise err("fixed left width may only be used with fixed "			"right align")	if width_type == 'fixed right' and align_type != 'fixed left':		raise err("fixed right width may only be used with fixed "			"left align")	return align_type, align_amount, width_type, width_amountdef decompose_valign_height( valign, height, err ):	try:		if valign in ('top','middle','bottom'):			valign = (valign,0)		valign_type, valign_amount = valign		assert valign_type in ('top','middle','bottom','fixed top','fixed bottom','relative')	except:		raise err, "Invalid valign: %s" % `valign`	try:		if height is None:			height = None, None		elif type(height) == type(0):			height=('fixed',height)		height_type, height_amount = height		assert height_type in (None, 'fixed','fixed bottom','fixed top','relative')	except:		raise err, "Invalid height: %s"%`height`			if height_type == 'fixed top' and valign_type != 'fixed bottom':		raise err, "fixed top height may only be used with fixed bottom valign"	if height_type == 'fixed bottom' and valign_type != 'fixed top':		raise err, "fixed bottom height may only be used with fixed top valign"			return valign_type, valign_amount, height_type, height_amountdef calculate_filler( valign_type, valign_amount, height_type, height_amount, 		      min_height, maxrow ):	if height_type == 'fixed':		height = height_amount	elif height_type == 'relative':		height = int(height_amount*maxrow/100+.5)		if min_height is not None:			    height = max(height, min_height)	else:		assert height_type in ('fixed bottom','fixed top')		height = maxrow-height_amount-valign_amount		if min_height is not None:			    height = max(height, min_height)		if height >= maxrow:		# use the full space (no padding)		return 0, 0			if valign_type == 'fixed top':		top = valign_amount		if top+height <= maxrow:			return top, maxrow-top-height		# need to shrink top		return maxrow-height, 0	elif valign_type == 'fixed bottom':		bottom = valign_amount		if bottom+height <= maxrow:			return maxrow-bottom-height, bottom		# need to shrink bottom		return 0, maxrow-height			elif valign_type == 'relative':		top = int( (maxrow-height)*valign_amount/100+.5 )	elif valign_type == 'bottom':		top = maxrow-height		elif valign_type == 'middle':		top = int( (maxrow-height)/2 )	else: #self.valign_type == 'top'		top = 0		if top+height > maxrow: top = maxrow-height	if top < 0: top = 0		bottom = maxrow-height-top	return top, bottom 	def calculate_padding( align_type, align_amount, width_type, width_amount,		min_width, maxcol, clip=False ):	if width_type == 'fixed':		width = width_amount	elif width_type == 'relative':		width = int(width_amount*maxcol/100+.5)		if min_width is not None:			    width = max(width, min_width)	else: 		assert width_type in ('fixed right', 'fixed left')		width = maxcol-width_amount-align_amount		if min_width is not None:			    width = max(width, min_width)		if width == maxcol or (width > maxcol and not clip):		# use the full space (no padding)		return 0, 0			if align_type == 'fixed left':		left = align_amount		if left+width <= maxcol:			return left, maxcol-left-width		# need to shrink left		return maxcol-width, 0	elif align_type == 'fixed right':		right = align_amount		if right+width <= maxcol:			return maxcol-right-width, right		# need to shrink right		return 0, maxcol-width			elif align_type == 'relative':		left = int( (maxcol-width)*align_amount/100+.5 )	elif align_type == 'right':		left = maxcol-width		elif align_type == 'center':		left = int( (maxcol-width)/2 )	else: 		assert align_type == 'left'		left = 0		if width < maxcol:		if left+width > maxcol: left = maxcol-width		if left < 0: left = 0		right = maxcol-width-left	return left, right 	class Frame(BoxWidget):	def __init__(self, body, header=None, footer=None, focus_part='body'):		"""		body -- a box widget for the body of the frame		header -- a flow widget for above the body (or None)		footer -- a flow widget for below the body (or None)		focus_part -- 'header', 'footer' or 'body'		"""		self.__super.__init__()		self._header = header		self._body = body		self._footer = footer		self.focus_part = focus_part		def get_header(self):		return self._header	def set_header(self, header):		self._header = header		self._invalidate()	header = property(get_header, set_header)			def get_body(self):		return self._body	def set_body(self, body):		self._body = body		self._invalidate()	body = property(get_body, set_body)	def get_footer(self):		return self._footer	def set_footer(self, footer):		self._footer = footer		self._invalidate()	footer = property(get_footer, set_footer)	def set_focus(self, part):

⌨️ 快捷键说明

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