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


Python Loader.loadStrategy方法代码示例

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


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

示例1: __init__

# 需要导入模块: from loader import Loader [as 别名]
# 或者: from loader.Loader import loadStrategy [as 别名]
class ConfigDialog:
    def __init__(self, result, config, defaults):
        self.result = result
        self.config = config
        self.lo = Loader()
        self.master = Tk()
        
        Label(self.master, text='Choose strategies (just close this window when ready):').grid(row=0, columnspan=2)
        # Create list of strategies.
        stratFiles = os.listdir('strategies')
        self.index = [self.lo.loadStrategy(filename[:-3]) for filename in stratFiles]
        # And it human-readable names.
        names = [o.name + ' ' + o.version for o in self.index]
        
        # Strategies combo box.
        self.selected = StringVar()
        self.selected.set(names[0])
        self.selected.trace('w', self.changeDescription)
        OptionMenu(self.master, self.selected, *names).grid(row=1)
        
        # Description field.
        self.desc = Text(self.master, width=1)
        self.desc.insert('0.0', self.index[0].description)
        self.desc.configure(state="disabled")
        self.desc.grid(row=2, sticky=NSEW)
        
        # "Add" button for strategies.
        Button(self.master, text='Add =>', command=self.addStrategy).grid(row=3)
        
        # Selected strategies list.
        self.stratList = Listbox(self.master)
        for name in defaults['strategies']:
            self.stratList.insert(END, name + ' ' + self.lo.loadStrategy(name).version)
        self.stratList.grid(row=1, column=1, rowspan=2, sticky=NS)
        
        # "Delete" button for strategies.
        Button(self.master, text='Delete', command=lambda lb=self.stratList: lb.delete(ANCHOR)).grid(row=3, column=1)
        
        # Themes combo box.
        Label(self.master, text='Choose theme:').grid(row=4, column=0, sticky=W)
        self.theme = StringVar()
        self.theme.set(defaults['theme'])
        OptionMenu(self.master, self.theme, *[filename[:-3] for filename in os.listdir('themes')]).grid(row=4, column=1, sticky=EW)
        
        # Width spinbox.
        Label(self.master, text='Field width:').grid(row=5, column=0, sticky=W)
        self.width = Spinbox(self.master, from_=10, to=300)
        self.width.delete(0, END)
        self.width.insert(0, defaults['width'])
        self.width.grid(row=5, column=1, sticky=EW)
        
        # Height spinbox.
        Label(self.master, text='Field height:').grid(row=6, column=0, sticky=W)
        self.height = Spinbox(self.master, from_=10, to=300)
        self.height.grid(row=6, column=1, sticky=EW)
        self.height.delete(0, END)
        self.height.insert(0, defaults['height'])
        
        # Delay spinbox.
        Label(self.master, text='Step delay (in ms):').grid(row=7, column=0, sticky=W)
        self.delay = Spinbox(self.master, from_=100, to=5000)
        self.delay.delete(0, END)
        self.delay.insert(0, defaults['delay'])
        self.delay.grid(row=7, column=1, sticky=EW)  
        
        # # Logs checkbox. (logs dont working anyway)
        # self.enable_logs = IntVar()
        # self.enable_logs.set(int(defaults['enable_logs']))
        # Checkbutton(self.master, text='Enable logs', variable=self.enable_logs).grid(row=8, column=0, columnspan=2, sticky=W)
        
        self.master.protocol('WM_DELETE_WINDOW', self.ok)
        self.master.mainloop()
        
    def addStrategy(self):
        self.stratList.insert(END, self.selected.get())
        
    def changeDescription(self, *nothing, **nowhere):
        names = [o.name + ' ' + o.version for o in self.index]
        self.desc.delete('0.0', END)
        self.desc.insert('0.0', self.index[names.index(self.selected.get())].description)

    def ok(self):
        names = [o.name + ' ' + o.version for o in self.index]
        for name in list(self.stratList.get(0, END)):
            self.result.append(self.index[names.index(name)])
        self.config['theme'] = (self.theme.get() if self.theme.get() else 'constructor')
        self.config['width'] = (int(self.width.get()) if self.width.get().isdigit() else 50)
        self.config['height'] = (int(self.height.get()) if self.height.get().isdigit() else 21)
        self.config['delay'] = (int(self.delay.get()) if self.delay.get().isdigit() else 500)
        
        # # Logs dont working anyway.
        # self.config['enable_logs'] = bool(self.enable_logs.get())
        self.config['enable_logs'] = False
        
        self.master.destroy()
开发者ID:galilei2000,项目名称:AntWars-1,代码行数:97,代码来源:ConfigDialog.py

示例2: __init__

# 需要导入模块: from loader import Loader [as 别名]
# 或者: from loader.Loader import loadStrategy [as 别名]
class ConfigDialog:
    def __init__(self, result, config, defaults):
        self.result = result
        self.config = config
        self.lo = Loader()
        self.master = Tk()

        Label(self.master, text='Choose strategies):').grid(
            row=0, columnspan=2)
        # Create list of strategies.
        stratFiles = filter(lambda t:
                                not t.endswith('.pyc')
                                and not t.startswith('__init__.py'),
                            os.listdir(strategies.__path__[0]))
        self.index = {
            make_human_readable_names(filename): self.lo.loadStrategy(os.path.join(strategies.__path__[0], filename)) for filename in stratFiles
        }
        names = [make_human_readable_names(f) for f in stratFiles]

        # todo: выпилить опцию "изменить описание"
        # Strategies combo box.
        self.selected = StringVar()
        self.selected.set(names[0])
        self.selected.trace('w', self.changeDescription)
        OptionMenu(self.master, self.selected, *names).grid(row=1)

        # Description field.
        self.desc = Text(self.master, width=1)
        self.desc.insert('0.0', sorted(self.index.values())[0].description)
        self.desc.grid(row=2, sticky=NSEW)

        # "Add" button for strategies.
        Button(self.master, text='Add =>',
               command=self.addStrategy).grid(row=3)

        # Selected strategies list.
        self.stratList = Listbox(self.master)
        for name in defaults['strategies']:
            self.stratList.insert(
                END, name)
        self.stratList.grid(row=1, column=1, rowspan=2, sticky=NS)

        # "Delete" button for strategies.
        Button(self.master, text='Delete', command=lambda lb=self.stratList: lb.delete(
            ANCHOR)).grid(row=3, column=1)

        # Themes combo box.
        Label(self.master, text='Choose theme:').grid(
            row=4, column=0, sticky=W)
        self.theme = StringVar()
        self.theme.set(defaults['theme'])

        def drop_extension(x):
            return x.split('.')[0]
        OptionMenu(
            self.master,
            self.theme,
            *[drop_extension(filename) for filename in filter(
                lambda t: t.endswith('.yml'),
                os.listdir(themes.__path__[0])
            )]
        ).grid(row=4, column=1, sticky=EW)

        # Width spinbox.
        Label(self.master, text='Field width:').grid(row=5, column=0, sticky=W)
        self.width = Spinbox(self.master, from_=10, to=300)
        self.width.delete(0, END)
        self.width.insert(0, defaults['width'])
        self.width.grid(row=5, column=1, sticky=EW)

        # Height spinbox.
        Label(self.master, text='Field height:').grid(
            row=6, column=0, sticky=W)
        self.height = Spinbox(self.master, from_=10, to=300)
        self.height.grid(row=6, column=1, sticky=EW)
        self.height.delete(0, END)
        self.height.insert(0, defaults['height'])

        # Delay spinbox.
        Label(self.master, text='Step delay (in ms):').grid(
            row=7, column=0, sticky=W)
        self.delay = Spinbox(self.master, from_=100, to=5000)
        self.delay.delete(0, END)
        self.delay.insert(0, defaults['delay'])
        self.delay.grid(row=7, column=1, sticky=EW)

        # destroy window and start game
        Button(self.master, text='  Launch battle!  ', command=self.launch_game).grid(row=9, columnspan=3, rowspan=2, sticky=S)

        self.master.mainloop()

    def addStrategy(self):
        name = self.selected.get()
        dir_path = os.path.split(strategies.__path__[0])[-1]
        self.stratList.insert(END, os.path.join(dir_path, name + '.py'))

    def changeDescription(self, *nothing, **nowhere):
        self.desc.delete('0.0', END)
        self.desc.insert('0.0', self.index[self.selected.get()].description)

#.........这里部分代码省略.........
开发者ID:AntWars,项目名称:AntWars,代码行数:103,代码来源:ConfigDialog.py


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