简体   繁体   中英

Quiz Python Creating new window

How would i make this so when one of the buttons is clicked it brings up a new window or deletes everything in the window and replaces it with questions and answers:

import tkinter
from tkinter import*
from tkinter import ttk
from tkinter import messagebox

def open_msg_box():
    messagebox.showinfo(
        "Event Triggered",
        "Button Clicked"
    )

root= Tk()
root.geometry("900x150+200+250")
root.configure(background = "Light blue")

frame = Frame(root)
style = ttk.Style()

style.configure("TButton",
                foreground="midnight blue",
                font="Times 20 bold italic",
                padding=20)
style.configure("TLabel",
                foreground="midnight blue",
                font="Times 20 bold italic",
                padding=20)

question = ttk.Label(root, text="Choose a Topic").pack(side=TOP)
theButton= ttk.Button(root, text="History", 
command=open_msg_box).pack(side=LEFT)
theButton2= ttk.Button(root, text="Music", 
command=open_msg_box).pack(side=RIGHT)
theButton3= ttk.Button(root, text="Computer Science", 
command=open_msg_box).pack()


frame.pack()

root.mainloop()

there could be many ways to do this, and there are. but as you asked these two answers could do it as you may want, the first one will open the next window without deleting the first one and the second way will show you how you could make it by destroying the last window when opening the new one.

import tkinter
from tkinter import*
from tkinter import ttk
class way1:
    def __init__(self):
        root= Tk()
        self.root=root
        root.geometry("900x150+200+250")
        root.configure(background = "Light blue")

        frame = Frame(root)
        style = ttk.Style()

        style.configure("TButton",foreground="midnight blue",font="Times 20 bold italic",padding=20)
        style.configure("TLabel",foreground="midnight blue",font="Times 20 bold italic",padding=20)

        question = ttk.Label(root, text="Choose a Topic").pack(side=TOP)
        theButton= ttk.Button(root, text="History", command=self.histroy_quetsion).pack(side=LEFT)
        theButton2= ttk.Button(root, text="Music",command=self.Music_quetsion).pack(side=RIGHT)
        theButton3= ttk.Button(root, text="Computer Science", command=self.Computer_quetsion).pack()


        frame.pack()

        root.mainloop()
    def histroy_quetsion(self):
        histroy=Toplevel(self.root)
        question = ttk.Label(histroy, text="These is the histroy quetsion and answer page").pack(side=TOP)
    def Music_quetsion(self):
        Music=Toplevel(self.root)
        question = ttk.Label(Music, text="These is music quetsion and answer page").pack(side=TOP)
    def Computer_quetsion(self):
        Computer=Toplevel(self.root)
        question = ttk.Label(Computer, text="These is the coumputer science quetsion and answer page").pack(side=TOP)
way1() 

class way2:
    def __init__(self):
        root= Tk()
        self.root=root
        root.geometry("900x150+200+250")
        root.configure(background = "Light blue")

        frame = Frame(root)
        style = ttk.Style()

        style.configure("TButton",foreground="midnight blue",font="Times 20 bold italic",padding=20)
        style.configure("TLabel",foreground="midnight blue",font="Times 20 bold italic",padding=20)

        question = ttk.Label(root, text="Choose a Topic").pack(side=TOP)
        theButton= ttk.Button(root, text="History", command=self.histroy_quetsion).pack(side=LEFT)
        theButton2= ttk.Button(root, text="Music",command=self.Music_quetsion).pack(side=RIGHT)
        theButton3= ttk.Button(root, text="Computer Science", command=self.Computer_quetsion).pack()


        frame.pack()

        root.mainloop()
    def histroy_quetsion(self):
        self.root.destroy()
        root=Tk()
        question = ttk.Label(root, text="These is the histroy quetsion and answer page").pack(side=TOP)
    def Music_quetsion(self):
        self.root.destroy()
        root=Tk()
        question = ttk.Label(root, text="These is music quetsion and answer page").pack(side=TOP)
    def Computer_quetsion(self):
        self.root.destroy()
        root=Tk()
        question = ttk.Label(root, text="These is the coumputer science quetsion and answer page").pack(side=TOP)
way2()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM