测验Python创建新窗口

时间:2017-09-30 19:29:21

标签: python tkinter

我如何做到这一点,当点击其中一个按钮时,它会弹出一个新窗口或删除窗口中的所有内容并用问题和答案替换它:

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

1 个答案:

答案 0 :(得分:0)

可能有很多方法可以做到这一点,而且有。但是当你问这两个答案可以按照你想要的那样做时,第一个将打开下一个窗口而不删除第一个窗口,第二个方法将告诉你如何通过在打开新窗口时摧毁最后一个窗口来实现它

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