当前位置: 首页>>代码示例>>Python>>正文


Python BooleanVar.trace方法代码示例

本文整理汇总了Python中Tkinter.BooleanVar.trace方法的典型用法代码示例。如果您正苦于以下问题:Python BooleanVar.trace方法的具体用法?Python BooleanVar.trace怎么用?Python BooleanVar.trace使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Tkinter.BooleanVar的用法示例。


在下文中一共展示了BooleanVar.trace方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: CellularAutomataMain

# 需要导入模块: from Tkinter import BooleanVar [as 别名]
# 或者: from Tkinter.BooleanVar import trace [as 别名]
class CellularAutomataMain(Tk):
    GRID_WIDTH_PX = DisplayConfiguration.CA_CANVAS_MAX_PIX_X
    GRID_HEIGHT_PX = DisplayConfiguration.CA_CANVAS_MAX_PIX_Y

    def __init__(self, width_cells=DisplayConfiguration.DEFAULT_WIDTH, *args, **kwargs):
        Tk.__init__(self, *args, **kwargs)
        self.resizable(width=False, height=False)
        self.state = {'rules': None, 'rules_file': None, 'grid': None}

        self.random_start = BooleanVar(self)
        self.random_start.trace('w', self.random_start_callback)

        #Load blank grid
        self.width_cells = width_cells
        self.height_cells = width_cells  # Default setting should probably have the grid be square.
        canvas_width = self.GRID_WIDTH_PX-80
        canvas_height = self.GRID_HEIGHT_PX-80
        self.state['grid'] = build_blank_grid(width_cells, width_cells)
        self.grid_display = GridDisplay(self, self.state['grid'],
                                        self.GRID_WIDTH_PX, self.GRID_HEIGHT_PX,
                                        width=canvas_width, height=canvas_height, relief=GROOVE, bd=4)
        self.grid_display.grid(row=0, column=0, padx=20, pady=20)

        #Build top menus
        self.menubar = Menu(self)
        try:
            self.config(menu=self.Menu)
        except AttributeError:
            self.tk.call(self, 'config', '-menu', self.menubar)
        self.file_menu = Menu(self, tearoff=False)
        self.file_menu.add_command(label="Load Ruleset", command=self.load_dialogue)
        self.file_menu.add_separator()
        self.file_menu.add_command(label="Save Automata (Image)", command=self.save_image_dialogue)
        self.file_menu.add_separator()
        self.file_menu.add_command(label="Quit", command=self.quit)
        self.menubar.add_cascade(menu=self.file_menu, label='File')

        self.config_menu = Menu(self, tearoff=False)
        self.config_menu.add_command(label='Set Dimensions', command=self.config_dimensions)
        self.config_menu.add_checkbutton(label='Set Grid Wrap')
        self.config_menu.add_checkbutton(label='Random Start Row', variable=self.random_start)
        self.bg_color_menu = self.build_selector_menu(GridDisplay.ALLOWED_COLORS, self.grid_display.set_bg_color,
                                                      lambda: self.grid_display.draw_grid(self.state['grid']), 'white')
        self.fill_color_menu = self.build_selector_menu(GridDisplay.ALLOWED_COLORS, self.grid_display.set_fill_color,
                                                        lambda: self.grid_display.draw_grid(self.state['grid']), 'black')
        self.config_menu.add_cascade(menu=self.bg_color_menu, label="Set Background Color...")
        self.config_menu.add_cascade(menu=self.fill_color_menu, label="Set Fill Color...")
        self.config_menu.add_separator()
        self.config_menu.add_command(label='Configure Plug-ins')
        self.plugin_menu = Menu(self)
        self.config_menu.add_cascade(menu=self.plugin_menu, label="Plugins...")
        self.menubar.add_cascade(menu=self.config_menu, label='Configure')
        self.menubar.add_command(label="About", command=self.about)

        #Load status bar
        self.status_bar_var = StringVar(self)
        self.status_bar_var.set('Initialized.')
        self.status_bar = Label(self, textvar=self.status_bar_var, relief=SUNKEN, justify=LEFT, anchor=W)
        self.status_bar.grid(row=1, column=0, sticky="EW", padx=2, pady=2)


        #Build plugin manager
        disp_logger.info('Loading plug-in manager')
        self.status_bar_var.set('Loading plug-in manager...')
        self.plugin_manager = PluginManager()
        self.plugin_manager.setPluginPlaces(['../plugins/'])
        self.plugin_manager.collectPlugins()
        disp_logger.info('Plug-in manager loaded. {} plug-ins were loaded.'
                         ''.format(len(self.plugin_manager.getAllPlugins())))

    def load_dialogue(self):
        new_rule_filename = tkFileDialog.askopenfilename(parent=self, title='Open Rule File', defaultextension=".txt",
                                                         filetypes=[('Rules Files', '*.txt'), ('All Files', '*')])
        if not new_rule_filename:
            return
        try:
            self.load(new_rule_filename)
        except:
            disp_logger.exception('Faulted loading rules file!')

    def save_image_dialogue(self):
        automata_image_filename = tkFileDialog.asksaveasfilename(parent=self, title='Save Automata Image',
                                                                 defaultextension='.eps',
                                                                 filetypes=[('Postscript', '.eps'),
                                                                            ('PNG', '.png'),
                                                                            ('JPEG', '.jpg')])
        if not automata_image_filename:
            return
        try:

            #TODO: Should we be converting, or should I have a local image representation instead?
            if not automata_image_filename.endswith('.eps'):
                self.grid_display.postscript(file='tmp.eps')
                img = Image.open('tmp.eps')
                img.save(automata_image_filename)
                os.remove('tmp.eps')
            else:
                self.grid_display.postscript(file=automata_image_filename)
            self.status_bar_var.set('Saved automata image file as: {}'.format(automata_image_filename))
        except:
#.........这里部分代码省略.........
开发者ID:cparker1,项目名称:CellularAutomata,代码行数:103,代码来源:CAApplication.py

示例2: Cap

# 需要导入模块: from Tkinter import BooleanVar [as 别名]
# 或者: from Tkinter.BooleanVar import trace [as 别名]
class Cap(Frame):
	def __init__(self):
		' set defaults, create widgets, bind callbacks, start live view '
		self.root = Tk()
		# menu:
		self.menu = Menu(self.root)
		self.root.config(menu=self.menu)
		# bind global keypresses:
		self.root.bind('q', lambda e: self.root.quit())
		self.root.bind('x', lambda e: self.root.quit())
		self.root.bind('<Destroy>', self.do_stop_video)
		self.root.bind('<space>', self.do_single_shot)
		self.root.bind('<Return>', self.do_single_shot)
		self.root.bind('<Button-3>', self.do_single_shot)
		self.root.bind('<Left>', lambda e: self.degree.set(-90))
		self.root.bind('<Right>', lambda e: self.degree.set(90))
		self.root.bind('<Up>', lambda e: self.degree.set(0))
		self.root.bind('a', lambda e: self.autocontrast.set(not self.autocontrast.get()))
		self.root.bind('e', lambda e: self.equalize.set(not self.equalize.get()))
		self.root.bind('g', lambda e: self.grayscale.set(not self.grayscale.get()))
		self.root.bind('i', lambda e: self.invert.set(not self.invert.get()))
		self.root.bind('s', lambda e: self.solarize.set(not self.solarize.get()))
		# config:
		self.config = RawConfigParser()
		self.config.read('filmroller.conf')
		if not self.config.has_section('global'):
			self.config.add_section('global')
		self.video = None
		self.invert = BooleanVar(name='invert')
		self.invert.set(self.config_get('invert', True))
		self.invert.trace('w', self.do_configure)
		self.grayscale = BooleanVar(name='grayscale')
		self.grayscale.set(self.config_get('grayscale', False))
		self.grayscale.trace('w', self.do_configure)
		self.autocontrast = BooleanVar(name='autocontrast')
		self.autocontrast.set(self.config_get('autocontrast', True))
		self.autocontrast.trace('w', self.do_configure)
		self.equalize = BooleanVar(name='equalize')
		self.equalize.set(self.config_get('equalize', False))
		self.equalize.trace('w', self.do_configure)
		self.solarize = BooleanVar(name='solarize')
		self.solarize.set(self.config_get('solarize', False))
		self.solarize.trace('w', self.do_configure)
		self.degree = IntVar(name='degree')
		self.degree.set(0)
		self.filename = StringVar(name='filename')
		self.videodevice = StringVar(name='videodevice')
		dev_names = sorted(['/dev/{}'.format(x) for x in listdir('/dev') if x.startswith('video')])
		d = self.config_get('videodevice', dev_names[-1])
		if not d in dev_names:
			d = dev_names[-1]
		self.videodevice.set(d)
		self.videodevice.trace('w', self.do_configure)
		#
		self.path = 'filmroller'
		if not exists(self.path):
			makedirs(self.path)
		# create gui:
		Frame.__init__(self, self.root)
		self.grid()
		self.x_canvas = Canvas(self, width=640, height=640, )
		self.x_canvas.pack(side='top')
		self.x_canvas.bind('<Button-1>', self.do_change_rotation)
		Checkbutton(self, text='Invert', variable=self.invert).pack(side='left')
		Checkbutton(self, text='Gray', variable=self.grayscale).pack(side='left')
		Checkbutton(self, text='Auto', variable=self.autocontrast).pack(side='left')
		OptionMenu(self, self.videodevice, *dev_names, command=self.restart_video).pack(side='left')
		Button(self, text='First role', command=self.do_first_role).pack(side='left')
		Label(self, textvariable=self.filename).pack(side='left')
		Button(self, text='Next role', command=self.do_inc_role).pack(side='left')
		Button(self, text='Take!', command=self.do_single_shot).pack(side='right')
		#filemenu = Menu(self.menu)
		#self.menu.add_cascade(label=self.videodevice.get(), menu=filemenu, )
		#for n in dev_names:
		#	filemenu.add_command(label=n, )
		#filemenu.add_separator()
		# start operation:
		self.do_first_role()
		self.do_start_video()

	def do_change_rotation(self, event):
		' determine where the image was clicked and turn that to the top '
		if event.x < 200:
			self.degree.set(-90)
		elif event.x > 640 - 200:
			self.degree.set(90)
		else:
			self.degree.set(0)

	def config_get(self, name, default):
		' read a configuration entry, fallback to default if not already stored '
		if not self.config.has_option('global', name):
			return default
		if isinstance(default, bool):
			return self.config.getboolean('global', name)
		else:
			return self.config.get('global', name)

	def do_configure(self, name, mode, cbname):
		' change a configuration entry '
#.........这里部分代码省略.........
开发者ID:JimmyDurandWesolowski,项目名称:python-v4l2capture,代码行数:103,代码来源:filmroller.py


注:本文中的Tkinter.BooleanVar.trace方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。