在Python 3和Tkinter中通过点击按钮打开新窗口
这是我现在做的程序,但我遇到了一个问题……
我想知道,点击按钮1后,如何才能打开一个新窗口呢?
import sys
from tkinter import *
import tkinter as tk
def mhello1():
mlabel = Label(mGui, text='A1').pack()
def mhello2():
mlabel = Label(mGui, text='A2').pack()
def mhello3():
mlabel = Label(mGui, text='A3').pack()
def mhello4():
mlabel
return
def mAbout():
messagebox.showinfo(title="About",message="program")
return
def mQuit():
mExit = messagebox.askyesno(title="Quit",message="y/n")
if mExit > 0:
mGui.destroy()
return
mGui = Tk()
mGui.geometry('450x450+200+200')
mGui.title('program')
mGui.configure(bg='gray')
mlabel = Label(text='option:',fg='red',bg = 'blue').pack()
mbutton1 = Button(mGui,text ='Button1',command = mhello1, height=5, width=20).pack()
mbutton2 = Button(mGui,text ='Button2',command = mhello2, height=5, width=20).pack()
mbutton3 = Button(mGui,text ='Button3',command = mhello3, height=5, width=20).pack()
mbutton4 = Button(mGui,text ='Button4',command = mhello4, height=5, width=20).pack()
mlabel2 = Label(text='activity:',fg='red',bg = 'blue').pack()
menubar=Menu(mGui)
filemenu = Menu(menubar, tearoff = 0)
filemenu.add_command(label="qwer")
filemenu.add_command(label="quit",command = mQuit)
menubar.add_cascade(label="more options",menu=filemenu)
helpmenu = Menu(menubar, tearoff = 0)
helpmenu.add_command(label="Help Docs")
helpmenu.add_command(label="About", command = mAbout)
menubar.add_cascade(label="help",menu=helpmenu)
mGui.config(menu=menubar)
mGui.mainloop()
我试过这个程序,但它不工作:Python 3 和 tkinter 点击按钮打开新窗口
有没有办法不使用 tkinter 的 toplevel?
非常感谢 :)
2 个回答
1
如果你想使用消息框,可以使用下面的代码行
from tkinter import *
from tkinter import messagebox
4
因为你应该只创建一个主窗口,所以你需要使用一个叫做 Toplevel 的东西来打开一个新的窗口。
def mhello1():
toplevel = Toplevel()
toplevel.title('Another window')
toplevel.focus_set()