0

这是我现在制作的程序,但我有问题......当我点击按钮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 and tkinter opens new window by click the button

有没有办法不使用 tkinter 顶层?

Tnx 很多 :)

4

2 回答 2

4

由于您应该只创建一个根窗口,因此您必须使用Toplevel来打开一个新窗口。

def mhello1():
    toplevel = Toplevel()
    toplevel.title('Another window')
    toplevel.focus_set()
于 2013-03-23T17:14:47.257 回答
1

如果您想使用消息框,请使用下面的那些行

from tkinter import *
from tkinter import messagebox
于 2016-09-05T17:17:08.073 回答