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] Checking button click in Tkinter GalaxyCoyote 3 94 Yesterday, 03:28 AM
Last Post: GalaxyCoyote
  [Tkinter] Tkinter button help and general commands Iceman 1 146 Sep-01-2019, 05:09 PM
Last Post: Yoriz
  [Tkinter] Extrakt a Variable from a closed tkinter window hWp 5 232 Aug-23-2019, 09:01 PM
Last Post: woooee
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 298 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  [Tkinter] RE: status bar to return to the centre after 1 minute of clicking a button ? chano 6 477 May-27-2019, 04:24 PM
Last Post: Yoriz
  [Tkinter] Tkinter window pop up again when i click button Orimura_Sandy 1 416 May-12-2019, 08:17 PM
Last Post: joe_momma
  [Tkinter] Top Level Window - from tkinter import * francisco_neves2020 6 486 Apr-23-2019, 09:27 PM
Last Post: francisco_neves2020
  [WxPython] Adding a Window to a Button wxPython ShashankDS 4 464 Apr-23-2019, 06:53 PM
Last Post: Yoriz
  [Tkinter] Help with tkinter; images and button commands SheeppOSU 2 458 Mar-28-2019, 02:01 AM
Last Post: woooee
  [Tkinter] Adding New TAB to NoteBook Widget by Clicking Vicolas 0 373 Feb-15-2019, 06:03 PM
Last Post: Vicolas

Forum Jump:


Users browsing this thread: 1 Guest(s)