测验 Python 创建新风

2024-04-26 18:53:00 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

Tags: textfromimportboxstyletkinterconfigurebutton
1条回答
网友
1楼 · 发布于 2024-04-26 18:53:00

有很多方法可以做到这一点。但是,当你问这两个答案可以做你可能想做的,第一个将打开下一个窗口,而不删除第一个和第二种方式将向你展示如何通过破坏最后一个窗口时,打开新的。你知道吗

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

相关问题 更多 >