python 3和tkinter中的新窗口,单击按钮

时间:2013-03-23 16:06:54

标签: python button tkinter

这是我现在制作的节目,但我有问题...... 当我点击button1然后打开新窗口

时,我该怎么做?
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 opening new window by clicking the button

有没有办法让我不使用tkinter toplevel?

Tnx很多:)

2 个答案:

答案 0 :(得分:3)

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

def mhello1():
    toplevel = Toplevel()
    toplevel.title('Another window')
    toplevel.focus_set()

答案 1 :(得分:1)

如果您想使用messagebox,请使用以下行

from tkinter import *
from tkinter import messagebox
相关问题