如何在python GUI中更新变量?

时间:2016-10-16 03:32:27

标签: python user-interface

我一直在努力进行GUI标签更新,这是一款简单的游戏,需要我完成其GUI,显示玩家得分和掉期数量的标签不会更改。如果有人能回答我的问题,我会感激不尽。

class StatusBar(tk.Frame):
def __init__(self,master):
    super().__init__(master)
    self._simple_player=SimplePlayer()

    self._swaps_label=tk.Label(self,
                               text=self._simple_player.get_swaps())
    self._score_label=tk.Label(self,
                               text=self._simple_player.get_score())
    self._swaps_label.pack(side=tk.RIGHT)
    self._score_label.pack(side=tk.RIGHT)

class App(object):
def __init__(self, master):
    """
    Constructor(SimpleTileApp, tk.Frame)
    """
    self._master = master
    self._simple_player=SimplePlayer()
    self._game = SimpleGame()
    self._status=StatusBar(master)
    self._status.pack()

    self._grid_view = TileGridView(
        master, self._game.get_grid(),
        width=GRID_WIDTH, height=GRID_HEIGHT, bg='red')
    self._grid_view.pack(side=tk.TOP)

我已尝试过Google上的所有方法,但没有任何帮助

我在类中重写一个函数来创建一个新标签,但它似乎没用了

class StatusBar(tk.Frame):
def __init__(self,master):
    super().__init__(master)
    self._simple_player=SimplePlayer()
    self._swaps_label=tk.Label(self,
                               text=self._simple_player.get_swaps())
    self._score_label=tk.Label(self,
                               text=self._simple_player.get_score())
    self._swaps_label.pack(side=tk.RIGHT)
    self._score_label.pack(side=tk.RIGHT)


def update_label(self):
    if self._simple_player.get_swaps()==-1:
        pass
    else:

        self._swaps_label=tk.Label(self,
                               text=self._simple_player.get_swaps())
        self._score_label=tk.Label(self,
                               text=self._simple_player.get_score())
        self._swaps_label.pack(side=tk.RIGHT)
        self._score_label.pack(side=tk.RIGHT)

0 个答案:

没有答案