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
  Scrollable big image in a window (TKinter) Prospekteur 3 136 Sep-14-2020, 03:06 AM
Last Post: Larz60+
  Run more than one window together with tkinter SmukasPlays 1 152 Aug-24-2020, 05:00 PM
Last Post: Larz60+
  Adding an image to a tkinter window djwilson0495 2 369 Aug-23-2020, 11:07 AM
Last Post: ebolisa
  [Tkinter] button command tkinter Heyjoe 6 1,200 Jul-30-2020, 07:06 PM
Last Post: deanhystad
  [Tkinter] calling a new window from a tkinter window neuroprogrammer 2 219 Jul-28-2020, 10:59 PM
Last Post: deanhystad
  How to display results from terminal window onto tkinter. buttercup 0 146 Jul-21-2020, 04:41 AM
Last Post: buttercup
  [tkinter] color change for hovering over button teacher 4 370 Jul-04-2020, 06:33 AM
Last Post: teacher
  tkinter button help Gina92 10 565 May-27-2020, 08:17 AM
Last Post: Gina92
  How to make button text bold in Tkinter? scratchmyhead 2 459 May-16-2020, 02:53 AM
Last Post: scratchmyhead
  [Tkinter] Tkinter adding entry values scratchmyhead 1 294 May-04-2020, 05:21 AM
Last Post: Yoriz

Forum Jump:


Users browsing this thread: 1 Guest(s)