Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何为Tkinter G.U.I按钮提供创建高分的功能?[更新]_Python_Python 2.7_Tkinter - Fatal编程技术网

Python 如何为Tkinter G.U.I按钮提供创建高分的功能?[更新]

Python 如何为Tkinter G.U.I按钮提供创建高分的功能?[更新],python,python-2.7,tkinter,Python,Python 2.7,Tkinter,我想学习如何在GUI按钮上输入高分函数。 我有一个创建和保存highscore的代码,但我想在按钮上实现它。 这是我的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")

我想学习如何在GUI按钮上输入高分函数。 我有一个创建和保存highscore的代码,但我想在按钮上实现它。 这是我的
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
代码,并将其放入另一个
def high_score()
函数中。我尝试使用GUI按钮运行命令关闭
def high_score()
函数,但它不能正常工作

这是我对代码的尝试:

import Tkinter as tk

def high_score():
    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


    # 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.")
    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.")
    if __name__ == "__main__":
        main()
# Make a top level Tk window
root = tk.Tk()
root.title("Cracker GUI v.01")
#Make button which takes highscore as a command
mega_button = tk.Button(root, text="Highscore", command=high_score)
mega_button.pack(side=tk.LEFT)
button = tk.Button(root, text='Quit', width=10, command=root.destroy)
button.pack()
#Lets get the show on the road
root.mainloop()

向我们展示您如何尝试创建名为
high\u score
的按钮。因此,我创建了一个本应启动“high\u score”程序的按钮,但该按钮无法正常工作,或者我只是不知道如何操作。我用我的编码尝试对其进行了更新。您可以测试它以查看我的错误。如果您能给我一些建议并批评我的代码,我将不胜感激。