Tkinter 中的按钮命令

2024-01-11

我正在尝试使用 tkinter 进行一次文本冒险,并且正在慢慢地将一些东西组合在一起。我试图显示从一个房间到另一个房间的命令,但即使按钮出现,当我按下它们时什么也没有发生。

game.py

#!/usr/bin/python
# -*- coding: utf-8 -*-

import world
from player import Player
from 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 as tk
from tkinter import ttk
import world, tiles, action
from 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)
        for 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 the tile at the given coordinates or None if there is no tile.

        :param x: the x-coordinate in the worldspace
        :param y: the y-coordinate in the worldspace
        :return: the tile at the given coordinates or None if there is no tile
        """
        return _world.get((x, y))

def load_tiles():
    with open('scenes.txt', 'r') as f:
        rows = f.readlines()
    x_max = len(rows[0].split('\t'))
    for y in range(len(rows)):
        cols = rows[y].split('\t')
        for 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
from 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 all of the default avaliable_actions in a 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 to {}, the shittiest place on earth.'.format(self.location)
        self.short_desc = 'Eh, I 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 is {}, but noone gives a damn.'.format(self.location)
        self.short_desc = 'Eh, I 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 for 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):
        for 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 -*-

from player import Player

class Action():
    def __init__(self, method, name, **kwargs):
        """Creates a new action

        :param method: the function object to execute
        :param name: the name of the action
        :param ends_turn: True if the player is expected to move after this action else False
        :param hotkey: The keyboard key the player should use to initiate this 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 the 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期望函数名称不带()和论点。

错误:

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

这样你就可以分配给command返回值player.do_action()但这个函数返回None

你必须使用lambda功能

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

但也许你还需要参数lambda因为你在中创建了这个for loop.

command=lambda act=action, kws=action.kwargs : player.do_action(act, **kws)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Tkinter 中的按钮命令 的相关文章

随机推荐

  • AWS 快照和 AMI 之间的区别

    所以我很难弄清楚这两者之间的具体区别是什么 据我了解 快照只是磁盘驱动器的备份 而 AMI 是整个系统 或者我应该说的实例 的备份 但整个系统在技术上不是完全位于磁盘驱动器上吗 如果是这样的话 那么没有明显的区别 我错过了一些东西吗 有两种
  • 获取 AWS CodeBuild 的 GitHub git 分支

    我将 AWS CodeBuild 设置为从 GitHub 自动构建 其他 CI 服务为分支提供了环境变量 但我找不到适用于 AWS CodeBuild 的环境变量 有一个CODEBUILD SOURCE VERSION设置为pr 7 whe
  • Bash 不会自动完成文件(Tab)

    bash 中的自动完成功能 使用 Tab 适用于文件夹 但是not与文件 我运行的是 Ubuntu 13 10 我还没碰过我的 bashrc file 底部的部分是这样的 bashrc文件看起来 与bash 完成安装和更新的部分 enabl
  • Javascript深度复制对象[重复]

    这个问题在这里已经有答案了 可能的重复 克隆 JavaScript 对象最有效的方法是什么 https stackoverflow com questions 122102 what is the most efficient way to
  • 在 Qt 中显示图像以适合标签大小

    我已经尝试了几种在表单上显示图像的方法 但没有一个能按照我想要的方式工作 我读过很多地方 最简单的方法是创建标签并使用它来显示图像 我有一个标签 其大小由布局指定 但是如果我使用像素图将图像加载到其中 则标签的大小将调整为图像的大小 如果我
  • 如何使用 Boost 库智能指针管理对象生命周期?

    有一个场景我需要使用shared ptr和weak ptr智能指针来解决 两个线程 线程 1 和 2 正在使用名为 A 的共享对象 每个线程都有对该对象的引用 线程 1 决定删除对象 A 但同时线程 2 可能正在使用它 如果我使用share
  • SignalR 连续消息传递

    我有一个网络项目 需要从外部肥皂服务更新网页上的统计信息 日志 我决定采用的方法是使用 signalR 通过使用执行服务方法并将结果返回给所有连接的客户端的代码 该代码将连续执行 在服务调用之间有一定的延迟 我无法将所有部分放在一起 可能是
  • 为什么 Visual Studio Code Git 显示驱动器上的所有更改?

    我在 Macbook 上使用 Visual Studio Code 1 6 0 我发现 VS Code 的 Git 列出了大量更改 甚至是那些不在我的工作文件夹中的更改 当我将鼠标悬停在这些上时 我会得到一个工具提示 该文件位于当前工作区之
  • 按顺序拆分 Parallel.Foreach 循环上的负载

    我需要处理列表中的一百万个元素 将它们粗暴地扔进并行 ForEach只会让CPU饱和 相反 我将元素主列表分成几部分 并将子列表放入并行循环中 List
  • 我可以在 SSRS 中跨数据源“加入”吗?

    我有两个数据源 一个 Oracle 和一个 Sql Server 由于我之前的情况 就像我发现它时的情况一样 Oracle 数据库中的某些列包含来自 Sql Server 数据库中的查找表的 PK 我正在尝试创建一个 Sql Server
  • 如何在Notepad++中删除当前行并添加新行?

    如何在Notepad 中删除当前行并添加新行 这两个操作很常见 但我找不到任何关键设置如何在 Notepad 中执行它们 任何插件都可以完成这项工作吗 Ctrl L cuts the current line and Ctrl Shift
  • 我可以 gzip 压缩所有 html 内容(页面)吗

    我试图找出定义哪些页面应该进行 gzip 压缩以及何时发送纯 html 内容是否有任何原则 如果你们可以分享您在 gzip 压缩项目的一部分时所做的决定 那将会很有帮助 一个好主意是进行基准测试 对比数据下降的速度有多快 压缩得有多好 如果
  • MIPS:求 5 个整数的平均值

    客观的 根据用户输入 使用五个寄存器查找平均值 Example 1 3 2 9 4 Output 3 输出是 3 因为 3 8 是小数 我希望它是整数 我的目标是不使用数组 但我在添加所有 5 个寄存器时遇到问题 data prompt1
  • 多索引数据帧删除每组最大值的行

    我有一个像这样的多索引数据框 PID Fid x y A 1 2 3 2 6 1 3 4 6 B 1 3 5 2 2 4 3 5 7 我想删除每个患者 PID 具有最高 x 值的行 我需要获取一个包含剩余行和所有列的新数据框 以继续对这些数
  • 使用 Swift 将自定义“数字”图像添加到 SKLabelNode 作为 SpriteKit 中的分数

    我基本上想在 Singles 中添加自定义字体编号 带有图像 并使用 SpriteKit 在我的游戏中添加它们作为我的分数 这是字体图像样式 它们是从 0 到 9 的数字 一段时间以来我一直在试图解决这个问题 我不确定您是否可以将其添加到
  • 如何为 fllot 中的条形图赋予纯色

    我需要为条形图提供纯色 我已关注此链接条形图示例 http www saltycrane com blog 2010 03 jquery flot stacked bar chart example 但我想要提供纯色 而且我还需要自己改变颜
  • 如何获取第一次发生的事件,然后抑制事件 2 秒(RxJS)

    我认为 RxJS 应该完全适合抑制重复按钮点击 2 秒 然而 我在实施过程中遇到了困难 var button myButton button button toObservable click Throttle 2000 Wouldn t
  • 暂停和恢复 Android Repo 同步

    我正在尝试同步以下存储库 repo init u git github com SlimRoms platform manifest git b jb 问题是我大约 30 小时前就开始了仓库同步 但它仍然没有完成 我有 1Mbps 连接 我
  • 如何防止 Firefox 在按 ctrl 和 + 时放大?

    我正在尝试在 Firefox 中使用 ctrl 和 组合来为我们的 Web 应用程序执行不同的操作 当我们的 Web 应用程序正在侦听此事件时 如何防止 Firefox 缩放 我不想更改 Firefox 中的设置 但希望代码以某种方式执行此
  • Tkinter 中的按钮命令

    我正在尝试使用 tkinter 进行一次文本冒险 并且正在慢慢地将一些东西组合在一起 我试图显示从一个房间到另一个房间的命令 但即使按钮出现 当我按下它们时什么也没有发生 game py usr bin python coding utf