Python Tkinter秒表不在框架

时间:2016-04-19 09:55:07

标签: python tkinter

我正在尝试为我的反应游戏构建用户界面。我已将帧设置在彼此之上但是当我将秒表功能添加到其中一个帧时,启动按钮不起作用。我已经测试了重置按钮和退出按钮,它们工作正常。由于秒表无法启动,我无法测试暂停按钮。任何人都可以帮助我吗?

以下是框架和秒表的代码:

class LvlOneR(tk.Frame):
 def __init__(self, parent, controller):
    tk.Frame.__init__(self, parent)
    self.controller = controller
    label = tk.Label(self, text="Level One",
                     background='white',
                     foreground='dodger blue',
                     font=TITLE_FONT)
    label.pack(side="top", fill="x", pady=10)
    #stopwatch 
    def update_timeText():
        if (state):
            global timer
            # Every time this function is called,
            # we will increment 1 centisecond (1/100 of a second)
            timer[2] += 1
            # Every 100 centisecond is equal to 1 second
            if (timer[2] >= 100):
                timer[2] = 0
                timer[1] += 1
            # Every 60 seconds is equal to 1 min
            if (timer[1] >= 60):
                timer[0] += 1
                timer[1] = 0
            # We create our time string here
            timeString = pattern.format(timer[0], timer[1], timer[2])
            # Update the timeText Label box with the current time
            timeText.configure(text=timeString)
            # Call the update_timeText() function after 1 centisecond
        self.after(10, update_timeText)
    # To start game
    def start():
        global state
        state = True

    # To pause the game
    def pause():
        global state
        state = False

    # To reset the timer to 00:00:00
    def reset():
        global timer
        timer = [0, 0, 0]
        timeText.configure(text='00:00:00')

    # To leave game our program

    def leave():
        controller.show_frame("ReactionGame")

    # Simple status flag
    # False mean the timer is not running
    # True means the timer is running (counting)
    state = False 

    # Our time structure [min, sec, centsec]
    timer = [0, 0, 0]
    # The format is padding all the
    pattern = '{0:02d}:{1:02d}:{2:02d}'
    # Create a timeText Label (a text box)
    timeText = tk.Label(self, text="00:00:00",
                        background='white',
                        foreground='dodger blue',
                        font=("Helvetica", 150))

    startButton = tk.Button(self, text='Start', command=start)


    pauseButton = tk.Button(self, text='Pause', command=pause)

    resetButton = tk.Button(self, text='Reset', command=reset)

    quitButton = tk.Button(self, text='Quit', command=leave)

    timeText.pack()
    startButton.pack()
    pauseButton.pack()
    resetButton.pack()
    quitButton.pack()
    update_timeText()

以下是设置框架的代码:

class Game(tk.Tk):

def __init__(self, *args, **kwargs):
    tk.Tk.__init__(self, *args, **kwargs)

    # the container is where we'll stack a bunch of frames
    # on top of each other, then the one we want visible
    # will be raised above the others
    container = tk.Frame(self)
    container.pack(side="top", fill="both", expand=True)
    container.grid_rowconfigure(0, weight=1)
    container.grid_columnconfigure(0, weight=1)


    self.frames = {}
    for F in (LoginScreen, MainMenu, ReactionGame, MemoryGame, HighScores, LvlOneR):
        page_name = F.__name__
        frame = F(container, self)
        self.frames[page_name] = frame

        # put all of the pages in the same location;
        # the one on the top of the stacking order
        # will be the one that is visible.
        frame.grid(row=0, column=0, sticky="nsew")
        frame.configure(background='white')

    self.show_frame("LoginScreen")

def show_frame(self, page_name):
    '''Show a frame for the given page name'''
    frame = self.frames[page_name]
    frame.tkraise()                   

1 个答案:

答案 0 :(得分:0)

请阅读如何创建Minimal, Complete and Verifiable Example

我认为(没有违法行为)你没有试图破坏你的代码?

  • 小部件是否适合自己?
  • 错误何时开始出现? (某些级别的小部件放在其他人中?)

这就是调试的内容。将您的代码分解为单个元素并逐个验证它们。

对我而言,这看起来很像"这里是代码,它不起作用,为什么?"

我认为它什么也没做,因为你什么都没做启动计时器功能(至少这些函数都是在你的init函数中声明的 - 为什么?)

或者是压缩代码的缩进?

编辑1

刚认识到 - 你正在拥有循环依赖(父控制孩子和孩子控制父母) - 尽量避免这种情况,只有在真正有关键的理由时才这样做。

有(我认为98%的情况)处理方式不同,有以下优点:

  • 更具可读性
  • 更易于维护
  • 更容易调试

编辑2

state在哪里初始化?

state = False置于class LvlOneR(tk.Frame):之上 在global state

之后立即def update_timeText():

解决至少一个问题。你的函数会识别状态变量。