Python Forum
tkinter- adding a new window after clicking a button built on the gui - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/Forum-Python-Coding)
+--- Forum: GUI (https://python-forum.io/Forum-GUI)
+--- Thread: tkinter- adding a new window after clicking a button built on the gui (/Thread-tkinter-adding-a-new-window-after-clicking-a-button-built-on-the-gui)



tkinter- adding a new window after clicking a button built on the gui - ShashankDS - Apr-18-2019

Hey guys,I don't know ,much about Programming I but as of now,'m trying to design an interface wherein if I click a button it should open a new Window . Also I want to know how to add a +/- button as well in the new window. I hope you guys can give me some inputs to my doubt The current code is

from tkinter import font
import tkinter as tk
from tkinter import *
import datetime
#datetime.datetime(2019,1,6,15,8,24,78915)

win =tk.Tk()

myFont = font.Font(family = 'Arial', size = 14, weight = 'bold')
titlefont = font.Font(family = 'Arial', size = 18, weight = 'bold')
win.title("Welcome to Manchester United")
win.geometry('1000x600')
win.configure(background='white')
title = tk.Label(win, text="Main Menu",font = titlefont)
title.grid(row=1,column=3,padx=40,pady=10)
Datetitle = tk.Label(win, text="Day/Month/Year:",font = myFont)
Datetitle.grid(row=1,column=4,padx=50)
Timetitle = tk.Label(win, text="Time:",font = myFont)
Timetitle.grid(row=2,column=4,padx=50)


ConnectLogo=PhotoImage(file="Connect.png")
Connect  = Button(win,image=ConnectLogo,text = "Connect", font = myFont,height =100 , width = 100,compound=TOP,bg = "orange") 
Connect.grid(row=3,column=1,padx=50,pady=40)

FrequencyLogo=PhotoImage(file="Frequency.png")
Frequency = Button(win,image=FrequencyLogo, text = "Frequency", font = myFont, height = 100, width =180,compound=TOP,bg = "Yellow")
Frequency.grid(row=3,column=2,padx=10)

MaskLogo=PhotoImage(file="Mask.gif")
Mask = Button(win,image=MaskLogo, text = "Mask", font = myFont, height = 100, width =180,compound=TOP,bg = "yellow")
Mask.grid(row=3,column=3,padx=10)

ToneLogo=PhotoImage(file="Tone.gif")
Tone = Button(win,image=ToneLogo, text = "Tone", font = myFont, height = 100, width =180,compound=TOP,bg = "yellow")
Tone.grid(row=3,column=4,padx=10)

VolumeLogo=PhotoImage(file="Volume.gif")
Volume = Button(win,image=VolumeLogo, text = "Volume", font = myFont, height = 100, width =180,compound=TOP,bg = "yellow")
Volume.grid(row=4, column=2,padx=10,pady=15)

TestselectLogo=PhotoImage(file="Testselect.gif")
Testselect = Button(win,image=TestselectLogo, text = "Test Select", font = myFont, height = 100, width =190,compound=TOP,bg = "yellow")
Testselect.grid(row=4, column=3,padx=10,pady=15)

StirLogo=PhotoImage(file="StirLogo.png")
Stir = Button(win,image=StirLogo, text = "Programmed Stir", font = myFont, height = 100, width =210,compound=TOP,bg = "pink")
Stir.grid(row=4, column=4,padx=10,pady=15)

SettingsLogo=PhotoImage(file="SettingsLogo.png")
Settings = Button(win,image=SettingsLogo, text = "Settings", font = myFont, height = 100, width =100,compound=TOP,bg = "orange")
Settings.grid(row=5, column=1,padx=10,pady=15)


mainloop()
To add the new window I want to know whether the following code ca be used:
from Tkinter import *

class Window(Tk):
    def __init__(self, parent):
        Tk.__init__(self, parent)
        self.parent = parent
        self.initialize()

    def initialize(self):
        self.geometry("600x400+30+30")
        wButton = Button(self, text='text', command = self.OnButtonClick())
        wButton.pack()

    def OnButtonClick(self):
        top = Toplevel()
        top.title("title")
        top.geometry("300x150+30+30")
        topButton = Button(top, text="CLOSE", command = self.destroy)
        topButton.pack()


if __name__ == "__main__":
    window = Window(None)

    window.title("title")

    window.mainloop()



RE: tkinter- adding a new window after clicking a button built on the gui - Yoriz - Apr-18-2019

The second code does what you want with the following changes
fix the import by removing the capitol t and don't use * import
remove the () from command = self.OnButtonClick()

import tkinter as tk
 
class Window(tk.Tk):
    def __init__(self, parent):
        tk.Tk.__init__(self, parent)
        self.parent = parent
        self.initialize()
 
    def initialize(self):
        self.geometry("600x400+30+30")
        wButton = tk.Button(self, text='text', command = self.OnButtonClick)
        wButton.pack()
 
    def OnButtonClick(self):
        top = tk.Toplevel()
        top.title("title")
        top.geometry("300x150+30+30")
        topButton = tk.Button(top, text="CLOSE", command = top.destroy)
        topButton.pack()
 
 
if __name__ == "__main__":
    window = Window(None)
 
    window.title("title")
 
    window.mainloop()



RE: tkinter- adding a new window after clicking a button built on the gui - ShashankDS - Apr-18-2019

Thanks for the Quick Response!