python - 从列表中打开GUI

时间:2017-11-21 22:52:23

标签: python class tkinter

    import random
    from tkinter import *
    import tkinter as tk

    scores = []
    score = 0

    #introduction
    def start():
        print("             Welcome to Maths Smash                   \n")
        print("Complete the questions the quickest to get a high score\n")
        name = input("What is your name? ")
        print("Ok", name, "let's begin!")

    #menu
    def main():

        choice = None
        while choice !="0":
            print(
            """
            Maths Smash

            0 - Exit
            1 - Easy
            2 - Medium
            3 - Hard
            4 - Extreme
            5 - Dashboard
            """
            )

            choice = input("Choice: ")
            print()

        #exit
            if choice == "0":
                print("Good-bye!")
                break
        #easy
            elif choice == "1":
                print("                       Easy Level                            ")
                print("Complete the test within two minutes to get 10 extra points\n")            
                easy_level()
        #medium
            elif choice == "2":
                print("                       Medium Level                          ")
                print("Complete the test within two minutes to get 10 extra points\n")
                medium_level()
        #hard
            elif choice == "3":
                print("                       Hard Level                            ")
                print("Complete the test within two minutes to get 10 extra points\n")
                hard_level()
        #extreme
            elif choice == "4":
                print("                     Extreme Level                           ")
                print("Complete the test within two minutes to get 10 extra points\n")
                extreme_level()
        #teacher login
            elif choice == "5":
                print("Dashboard")
                dashboard(Frame)
        #if the input does not = to choices        
        else:
            print("Sorry but", choice, "isn't a vaild choice.")

    class dashboard(Frame):
        def __init__(self, master):
            super(dashboard, self).__init__(master)
            self.grid()
            self.main_menu()

        def main_menu(self):        
            #student dashboard button
            bttn1 = Button(self, text = "Student",
                           command=self.student, height = 2, width= 15)
            bttn1.grid()

            #teacher dashboard button
            bttn2 = Button(self, text = "Teacher",
                           command=self.teacher, height = 2, width= 15)
            bttn2.grid()

            #exit button
            bttn3 = Button(self, text = "Exit",
                   command=root.destroy, height = 2, width= 15)
            bttn3.grid()

            self.main_page_buttons = bttn1, bttn2, bttn3  # Save button widgets.

        def student(self):
            for button in self.main_page_buttons:  # Hide the main page Buttons.
                button.grid_forget()

            #view highscores button
            bttn1 = Button(self, text = "Highscores",
                           command=self.view_scores, height = 2, width= 15)
            bttn1.grid()

            #print score button
            bttn2 = Button(self, text = "Save Score",
                           command=self.save_score, height = 2, width= 15)
            bttn2.grid()

            #exit button
            bttn3 = Button(self, text = "Exit",
                   command=root.destroy, height = 2, width= 15)
            bttn3.grid()

            self.student_page_buttons = bttn1, bttn2, bttn3  # Save button widgets.

        def save_score(self):
            f = open("Maths Test Score.txt','w")
            score = input("What did you score?\n")
            f.write(name, score)
            f.close()


        def teacher(self):
            for button in self.main_page_buttons:  # Hide the main page Buttons.
                button.grid_forget()
            #add highscores button
            bttn1 = Button(self, text = "Add Highscore",
                           command=self.add_score, height = 2, width= 15)
            bttn1.grid()

            #remove score button
            bttn2 = Button(self, text = "Remove Highscore",
                           command=self.remove_score, height = 2, width= 15)
            bttn2.grid()

            #view highscores
            bttn3 = Button(self, text = "View Highscores",
                           command=self.view_scores, height = 2, width= 15)
            bttn3.grid()

            #exit button
            bttn4 = Button(self, text = "Exit",
                           command=root.destroy, height = 2, width= 15)
            bttn4.grid()

            self.teacher_page_buttons = bttn1, bttn2, bttn3, bttn4  # Save button widgets.

        def add_score(self):
            global scores
            score = int(input("What score do you want to add?\n"))
            scores.append(score)

        def remove_score(self):
            global scores
            score = int(input("Remove which score?\n"))
            if score in scores:
                scores.remove(score)

        def view_scores(self):
            global scores
            print("High Scores")
            for score in scores:
                print(score)     

    #calling functions
    start()
    main()

    #main for gui
    root = Tk()
    root.title("Dashboard")
    root.geometry("300x170")
    app = dashboard(root)
    root.mainloop()

现在尝试让它工作一段时间但是,我想要做的就是当用户输入选择时打开GUI#5;'但我不确定是否有一种不同的方式来打电话给某个班级或者其他什么东西,在网上找了一下看不出任何有用的例子来帮助你。任何帮助都会非常感激,欢呼。

1 个答案:

答案 0 :(得分:0)

此时我无法运行代码,但我发现了一个错误。

创建dashboard时,它需要父实例 - 例如root - 但在menu()中,您使用Frame这是类名,而不是实例。

menu()中,您必须在创建root之前创建dashboard,然后再使用root.mainloop()

elif choice == "5":
    print("Dashboard")
    root = Tk()
    root.title("Dashboard")
    root.geometry("300x170")
    app = dashboard(root)
    root.mainloop()

编辑:完整的工作代码。

因为现在rootmain()内的局部变量,所以dashboard我使用self.master代替root - 请参阅command=self.master.destroy

import random
from tkinter import *

scores = []
score = 0

#introduction
def start():
    print("             Welcome to Maths Smash                   \n")
    print("Complete the questions the quickest to get a high score\n")
    name = input("What is your name? ")
    print("Ok", name, "let's begin!")

#menu
def main():

    choice = None
    while choice !="0":
        print(
        """
        Maths Smash

        0 - Exit
        1 - Easy
        2 - Medium
        3 - Hard
        4 - Extreme
        5 - Dashboard
        """
        )

        choice = input("Choice: ")
        print()

    #exit
        if choice == "0":
            print("Good-bye!")
            break
    #easy
        elif choice == "1":
            print("                       Easy Level                            ")
            print("Complete the test within two minutes to get 10 extra points\n")            
            easy_level()
    #medium
        elif choice == "2":
            print("                       Medium Level                          ")
            print("Complete the test within two minutes to get 10 extra points\n")
            medium_level()
    #hard
        elif choice == "3":
            print("                       Hard Level                            ")
            print("Complete the test within two minutes to get 10 extra points\n")
            hard_level()
    #extreme
        elif choice == "4":
            print("                     Extreme Level                           ")
            print("Complete the test within two minutes to get 10 extra points\n")
            extreme_level()
    #teacher login
        elif choice == "5":
            print("Dashboard")

            root = Tk()
            root.title("Dashboard")
            root.geometry("300x170")
            app = Dashboard(root)
            root.mainloop()

        #if the input does not = to choices        
    else:
        print("Sorry but", choice, "isn't a vaild choice.")

class Dashboard(Frame):

    def __init__(self, master):
        super(Dashboard, self).__init__(master)
        self.grid()
        self.main_menu()

    def main_menu(self):        
        #student dashboard button
        bttn1 = Button(self, text = "Student",
                       command=self.student, height = 2, width= 15)
        bttn1.grid()

        #teacher dashboard button
        bttn2 = Button(self, text = "Teacher",
                       command=self.teacher, height = 2, width= 15)
        bttn2.grid()

        #exit button
        bttn3 = Button(self, text="Exit",
               command=self.master.destroy, height = 2, width= 15)
        bttn3.grid()

        self.main_page_buttons = bttn1, bttn2, bttn3  # Save button widgets.

    def student(self):
        for button in self.main_page_buttons:  # Hide the main page Buttons.
            button.grid_forget()

        #view highscores button
        bttn1 = Button(self, text = "Highscores",
                       command=self.view_scores, height = 2, width= 15)
        bttn1.grid()

        #print score button
        bttn2 = Button(self, text = "Save Score",
                       command=self.save_score, height = 2, width= 15)
        bttn2.grid()

        #exit button
        bttn3 = Button(self, text = "Exit",
               command=self.master.destroy, height = 2, width= 15)
        bttn3.grid()

        self.student_page_buttons = bttn1, bttn2, bttn3  # Save button widgets.

    def save_score(self):
        f = open("Maths Test Score.txt','w")
        score = input("What did you score?\n")
        f.write(name, score)
        f.close()


    def teacher(self):
        for button in self.main_page_buttons:  # Hide the main page Buttons.
            button.grid_forget()

        #add highscores button
        bttn1 = Button(self, text = "Add Highscore",
                       command=self.add_score, height = 2, width= 15)
        bttn1.grid()

        #remove score button
        bttn2 = Button(self, text = "Remove Highscore",
                       command=self.remove_score, height = 2, width= 15)
        bttn2.grid()

        #view highscores
        bttn3 = Button(self, text = "View Highscores",
                       command=self.view_scores, height = 2, width= 15)
        bttn3.grid()

        #exit button
        bttn4 = Button(self, text = "Exit",
                       command=self.master.destroy, height = 2, width= 15)
        bttn4.grid()

        self.teacher_page_buttons = bttn1, bttn2, bttn3, bttn4  # Save button widgets.

    def add_score(self):
        global scores

        score = int(input("What score do you want to add?\n"))
        scores.append(score)

    def remove_score(self):
        global scores

        score = int(input("Remove which score?\n"))
        if score in scores:
            scores.remove(score)

    def view_scores(self):
        global scores

        print("High Scores")
        for score in scores:
            print(score)     

#calling functions
start()
main()