python - Button commands in Tkinter -


i'm trying make text adventure tkinter , i'm getting together. i'm trying display commands come room room though buttons appear, nothing happens when press them.

game.py

#!/usr/bin/python # -*- coding: utf-8 -*-  import world player import player ui import *  def main():     gui = window(root())     while true:         gui.mainloop()     else:         pass  if __name__ == '__main__':     main() 

ui.py

#!/usr/bin/python # -*- coding: utf-8 -*-  import tkinter tk tkinter import ttk import world, tiles, action player import player   class window(tk.frame):     def __init__(self, master):         tk.frame.__init__(self, master=master)         self.master = master         self.player = player()         self.init_ui()      def init_ui(self):         self.master.title("****")         self.tabs = tabs(self.master)         world.load_tiles()         self.world = world.tile_exists(self.player.location_x, self.player.location_y)         self.update_main()      def update_main(self):         self.world.scene_init()         self.world.modify_player(self.player)         self.tabs.update_tab(self.world, self.player)      def _label(self, master, text, side=none, anchor=none):         new_label = tk.label(master, text=text)         new_label.pack(side=side, anchor=anchor)      def _button(self, master, text, command, side=none, anchor=none):         new_button = tk.button(master, text=text, command=command)         new_button.pack(side=side, anchor=anchor)   class tabs(window):     def __init__(self, master):         self.master = master         self.nb = ttk.notebook(self.master)         nb_1 = ttk.frame(self.nb)         self.frame_1 = tk.frame(nb_1, bg='red', bd=2, relief=tk.sunken, padx=5, pady=5)         self.frame_1.pack(expand=1, fill='both', side=tk.left)         self.nb.add(nb_1, text='game')         self.nb.pack(expand=1, fill='both', side=tk.left)      def update_tab(self, world, player):         avaliable_actions = world.avaliable_actions()         self._label(self.frame_1, world.display_text(), side=tk.left, anchor=tk.n)         action in avaliable_actions:             self._button(self.frame_1, text=action, command=player.do_action(action, **action.kwargs), side=tk.bottom, anchor=tk.e)   def root():     root = tk.tk()     root.geometry("600x350+200+200")     return root 

world.py

#!/usr/bin/python # -*- coding: utf-8 -*-  _world = {}  def tile_exists(x, y):         """returns tile @ given coordinates or none if there no tile.          :param x: x-coordinate in worldspace         :param y: y-coordinate in worldspace         :return: tile @ given coordinates or none if there no tile         """         return _world.get((x, y))  def load_tiles():     open('scenes.txt', 'r') f:         rows = f.readlines()     x_max = len(rows[0].split('\t'))     y in range(len(rows)):         cols = rows[y].split('\t')         x in range(x_max):             tile_name = cols[x].replace('\n', '')             _world[(x, y)] = none if tile_name == '' else getattr(__import__('tiles'),                 tile_name)(x, y)     return _world 

tiles.py

#!/usr/bin/python # -*- coding: utf-8 -*-  import world, action player import player   class maptile():     def __init__(self, x, y):         self.x = x         self.y = y       def display_text(self):         pass         # raise notimplementederror()      def modify_player(self, the_player):         raise notimplementederror()      def adjacent_moves(self):         moves = []         if world.tile_exists(self.x + 1, self.y):             moves.append(action.moveeast())         if world.tile_exists(self.x - 1, self.y):             moves.append(action.movewest())         if world.tile_exists(self.x, self.y - 1):             moves.append(action.movenorth())         if world.tile_exists(self.x, self.y + 1):             moves.append(action.movesouth())         return moves      def avaliable_actions(self):         '''returns of default avaliable_actions in room'''         moves = self.adjacent_moves()         # moves.append(action.viewinventory())         return moves  class scene_1(maptile):     def scene_init(self):         self.location = 'scene_1'         self.long_desc = 'welcome {}, shittiest place on earth.'.format(self.location)         self.short_desc = 'eh, don\'t care.'      def display_text(self):         return self.long_desc      def modify_player(self, the_player):         self.first = true         return self.display_text()  class scene_2(maptile):     def scene_init(self):         self.location = 'scene_2'         self.long_desc = 'this {}, noone gives damn.'.format(self.location)         self.short_desc = 'eh, don\'t care, really.'      def display_text(self):         return self.long_desc      def modify_player(self, the_player):         self.first = true         return self.display_text() 

player.py

#!/usr/bin/python # -*- coding: utf-8 -*-  class player():     '''base player'''     def __init__(self):         self.inventory = []         self.hp = 100         self.location_x, self.location_y = 1, 1         self.victory = false      def is_alive(self):         return self.hp >= 0      def do_action(self, action, **kwargs):         action_method = getattr(self, action.method.__name__)         if action_method:             action_method(**kwargs)      def print_inventory(self):         item in self.inventory:             print(item, 'n')      def move(self, dx, dy):         self.location_x += dx         self.location_y += dy      def move_north(self):         self.move(dx=0, dy=-1)      def move_south(self):         self.move(dx=0, dy=1)      def move_east(self):         self.move(dx=1, dy=0)      def move_west(self):         self.move(dx=-1, dy=0) 

action.py

#!/usr/bin/python # -*- coding: utf-8 -*-  player import player  class action():     def __init__(self, method, name, **kwargs):         """creates new action          :param method: function object execute         :param name: name of action         :param ends_turn: true if player expected move after action else false         :param hotkey: keyboard key player should use initiate action         """         self.method = method         self.name = name         self.kwargs = kwargs      def __str__(self):         return "{}".format(self.name)  class movenorth(action):     def __init__(self):         super().__init__(method=player.move_north, name='north')  class movesouth(action):     def __init__(self):         super().__init__(method=player.move_south, name='south')   class moveeast(action):     def __init__(self):         super().__init__(method=player.move_east, name='east')   class movewest(action):     def __init__(self):         super().__init__(method=player.move_west, name='west')   class viewinventory(action):     """prints player's inventory"""     def __init__(self):         super().__init__(method=player.print_inventory, name='view inventory', hotkey='i')   class attack(action):     def __init__(self, enemy):         super().__init__(method=player.attack, name="attack", hotkey='a', enemy=enemy)   class flee(action):     def __init__(self, tile):         super().__init__(method=player.flee, name="flee", hotkey='f', tile=tile) 

command expect function name without () , arguments.

mistake:

command=player.do_action(action, **action.kwargs) 

this way assign command value returned player.do_action() functions returns none

you have use lambda function

command=lambda:player.do_action(action, **action.kwargs) 

but maybe need arguments in lambda because create in for loop.

command=lambda act=action, kws=action.kwargs : player.do_action(act, **kws) 

Comments