joza jozic joza jozic - 3 months ago 19
Python Question

new window in python 3 and tkinter by clicking on button

This is program which I made for now, but I have problem...
how can I make when I click on button1 that then opens new window

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


I try this program but it doesn't work:
Python 3 and tkinter opening new window by clicking the button

is there a way that I don't use tkinter toplevel?

Tnx a lot :)

Answer

Since you should create only one root window, you have to use a Toplevel to open a new one.

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