【问题标题】:new window in python 3 and tkinter by clicking on button通过单击按钮在 python 3 和 tkinter 中的新窗口
【发布时间】:2013-03-23 16:06:54
【问题描述】:

这是我现在制作的程序,但我有问题...... 当我点击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 顶层?

Tnx 很多 :)

【问题讨论】:

    标签: python button tkinter


    【解决方案1】:

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

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

    【讨论】:

    • @jozajozic 很高兴它有帮助。如果它解决了您的问题,请随时接受答案。
    【解决方案2】:

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

    from tkinter import *
    from tkinter import messagebox
    

    【讨论】:

      猜你喜欢
      • 2013-02-07
      • 1970-01-01
      • 1970-01-01
      • 2020-10-03
      • 2023-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-31
      相关资源
      最近更新 更多