【问题标题】:Python Tkinter stopwatch not working on framePython Tkinter 秒表在框架上不起作用
【发布时间】:2016-04-19 09:55:06
【问题描述】:

我正在尝试为我的反应游戏构建用户界面。我已经在彼此之上设置了框架,但是当我将秒表功能添加到其中一个框架上时,开始按钮不起作用。我已经测试了重置按钮和退出按钮,它们工作正常。我无法测试暂停按钮,因为秒表没有启动。谁能帮帮我?

这是框架和秒表的代码:

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()                   

【问题讨论】:

    标签: python tkinter


    【解决方案1】:

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

    我想(无意冒犯)您没有尝试破坏您的代码?

    • 小部件是否为自己工作?
    • 什么时候开始出现错误? (将小部件放置在其他小部件中的特定级别?)

    这就是调试的意义所在。将您的代码分解为单个元素并一一验证它们。

    在我看来,这很像“这是代码,它不起作用,为什么?”

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

    或者是缩进混淆了代码?

    编辑 1

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

    有(我假设 98% 的情况)有不同的处理方法,具有以下优点:

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

    编辑 2

    state 在哪里初始化?

    state = False 放在class LvlOneR(tk.Frame): 之上 和global state 紧跟在def update_timeText(): 之后

    至少可以解决您的一个问题。您的函数将识别状态变量。

    【讨论】:

    • 对不起第一次使用这个。我应该放什么代码才能更容易看到?秒表本身就可以正常工作,只是在添加到我创建的帧时不行。我尝试了您的编辑 2,但如果我在 update_timeText 之后放置全局状态,它无法识别计时器变量
    • 视窗。我已经解决了这个问题。无论如何感谢您的帮助
    • 如果是这样,请让社区知道是什么解决了您的问题。您也可以回答自己的问题。
    猜你喜欢
    • 1970-01-01
    • 2020-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-04
    相关资源
    最近更新 更多