Bottom Page

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()
Quote
#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()
Quote
#3
Thanks for the Quick Response!
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  [tkinter] color change for hovering over button teacher 4 81 Jul-04-2020, 06:33 AM
Last Post: teacher
  tkinter button help Gina92 10 312 May-27-2020, 08:17 AM
Last Post: Gina92
  How to make button text bold in Tkinter? scratchmyhead 2 195 May-16-2020, 02:53 AM
Last Post: scratchmyhead
  [Tkinter] Tkinter adding entry values scratchmyhead 1 193 May-04-2020, 05:21 AM
Last Post: Yoriz
  Closing window on button click not working kenwatts275 4 233 May-03-2020, 01:59 PM
Last Post: deanhystad
  Tkinter: multitab window WizardMitMagi 3 264 Apr-29-2020, 02:40 PM
Last Post: deanhystad
  tkinter.TclError: bad window path name kenwatts275 3 460 Apr-26-2020, 08:16 PM
Last Post: kenwatts275
  [Tkinter] Is it possible to automate button generation in tkinter? FirePepi 3 305 Apr-10-2020, 12:14 PM
Last Post: Riddle
  Tkinter:Unable to bind and unbind function with a button shallanq 2 332 Mar-28-2020, 02:05 AM
Last Post: joe_momma
  Using Tkinter widgets on child window chewy1418 8 894 Feb-27-2020, 10:34 PM
Last Post: Marbelous

Forum Jump:


Users browsing this thread: 1 Guest(s)