如何给Tkinter G.U.I按钮一个创建高分的功能? [更新]

时间:2017-05-31 18:38:54

标签: python python-2.7 tkinter

我想学习如何在GUI按钮上输入高分功能。 我有一个创建和保存高分的代码,但我想将它实现到按钮上。 这是我的class Webpage(object): def __init__(self, title, link): self.title = title self.link = link ex1 = ('really, really, really long title that will be chopped off', 'example.com') print Webpage.ex1 title = (title[:50] + '..' if len(title) > 50 else title) 代码:

Highscore.py

输出:

def get_high_score():
    # Default high score
    high_score = 0

    # Try to read the high score from a file
    try:
        high_score_file = open("high_score.txt", "r")
        high_score = int(high_score_file.read())
        high_score_file.close()
        print("The high score is", high_score)
    except IOError:
        # Error reading file, no high score
        print("There is no high score yet.")
    except ValueError:
        # There's a file there, but we don't understand the number.
        print("I'm confused. Starting with no high score.")

    return high_score


def save_high_score(new_high_score):
    try:
        # Write the file to disk
        high_score_file = open("high_score.txt", "w")
        high_score_file.write(str(new_high_score))
        high_score_file.close()
    except IOError:
        # Hm, can't write it.
        print("Unable to save the high score.")


def main():
    """ Main program is here. """
    # Get the high score
    high_score = get_high_score()

    # Get the score from the current game
    current_score = 0
    try:
        # Ask the user for his/her score
        current_score = int(input("What is your score? "))
    except ValueError:
        # Error, can't turn what they typed into a number
        print("I don't understand what you typed.")

    # See if we have a new high score
    if current_score > high_score:
        # We do! Save to disk
        print("Yea! New high score!")
        save_high_score(current_score)
    else:
        print("Better luck next time.")
if __name__ == "__main__":
    main()

我尝试使用('The high score is', 121345) What is your score? `[input]` 代码并将其放入另一个Highscore.py函数中。我尝试使用GUI按钮运行命令关闭def high_score()功能,但它无法正常运行。

这是我对代码的尝试:

def high_score()

0 个答案:

没有答案