Python Forum
tkinter- adding a new window after clicking a button built on the gui
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
tkinter- adding a new window after clicking a button built on the gui
#1
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()
Reply
#2
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()
Reply
#3
Thanks for the Quick Response!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Interaction between Matplotlib window, Python prompt and TKinter window NorbertMoussy 3 342 Mar-17-2024, 09:37 AM
Last Post: deanhystad
  [Tkinter] TKinter Remove Button Frame Nu2Python 8 816 Jan-16-2024, 06:44 PM
Last Post: rob101
  tkinter - touchscreen, push the button like click the mouse John64 5 746 Jan-06-2024, 03:45 PM
Last Post: deanhystad
  Tkinter multiple windows in the same window tomro91 1 786 Oct-30-2023, 02:59 PM
Last Post: Larz60+
  Centering and adding a push button to a grid window, TKinter Edward_ 15 4,378 May-25-2023, 07:37 PM
Last Post: deanhystad
  [Tkinter] Open tkinter colorchooser at toplevel (so I can select/focus on either window) tabreturn 4 1,831 Jul-06-2022, 01:03 PM
Last Post: deanhystad
  [Tkinter] Background inactivity timer when tkinter window is not active DBox 4 2,864 Apr-16-2022, 04:04 PM
Last Post: DBox
  [Tkinter] Clicking on the button crashes the TK window ODOshmockenberg 1 2,197 Mar-10-2022, 05:18 PM
Last Post: deanhystad
  why my list changes to a string as I move to another window in tkinter? pymn 4 2,546 Feb-17-2022, 07:02 AM
Last Post: pymn
  Can't get tkinter button to change color based on changes in data dford 4 3,363 Feb-13-2022, 01:57 PM
Last Post: dford

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020