Python Forum
Multi windows in tkinter buttons not responding correctly
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multi windows in tkinter buttons not responding correctly
#4
This should be the correct indentation

    toggleButton1["text"] = ("Fireplace On")
NameError: name 'toggleButton1' is not defined

import logging
import datetime
import tkinter
from queue import Queue
from ABE_helpers import ABEHelpers
from ABE_IoPi import IoPi
import RPi.GPIO as GPIO
import time
from time import sleep, localtime, strftime
import http.client, urllib
from tkinter import*
import tkinter as tk
import io
import base64
from urllib.request import urlopen
import math
import threading 
import _thread
import os
from tkinter import font  as tkfont
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
pinList = [29, 31, 33, 35, 37, 32, 36, 38, 40]

for i in pinList: 
    GPIO.setup(i, GPIO.OUT)
    GPIO.output(i, GPIO.HIGH)

class SampleApp(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        self.title_font = tkfont.Font(family='Helvetica', size=18, weight="bold", slant="italic")
        self.title_font2 = tkfont.Font(family='Helvetica', size=10, weight="bold")
        self.title_font3 = tkfont.Font(family='Helvetica', size=12, weight="bold")
        self.title_font4 = tkfont.Font(family='Helvetica', size=10, weight="bold")

       
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        
        
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}
        for F in (StartPage, Mainfloor, Upstairs, Basement, Yard):
            page_name = F.__name__
            frame = F(parent=container, controller=self)
            self.frames[page_name] = frame

            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame("StartPage")

    def show_frame(self, page_name):
        '''Show a frame for the given page name'''
        frame = self.frames[page_name]
        frame.tkraise()




#####################################

#Fireplace
 
class Fireplace():
    
    def __init__(self):

        if (bus1.read_pin(15) == 1):
           bus1.write_pin(15, 0)
           toggleButton1["text"] = ("Fireplace On")
           
        else:
                bus1.write_pin(15, 1)
                toggleButton1["text"] = ("Fireplace Off")
                
####################################

class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller

        controller.attributes('-zoomed', True)
        label2 = tk.Label(self, text= "Select a Page to View",
                          font=controller.title_font4)
        label2.place(x=290, y=60)
        

        button1 = tk.Button(self, text="Main Floor", width=15, height=5,
                            font=controller.title_font2,
                            bg= 'white',
                            command=lambda: controller.show_frame("Mainfloor"))

        button2 = tk.Button(self, text="Upstairs", width=15, height=5,
                            font=controller.title_font2,
                            bg= 'white',
                            command=lambda: controller.show_frame("Upstairs"))
                            
        button3 = tk.Button(self, text="Basement", width=15, height=5,
                            font=controller.title_font2,
                            bg= 'white',
                            command=lambda: controller.show_frame("Basement"))

        button4 = tk.Button(self, text="Yard Lighting", width=15, height=5,
                            font=controller.title_font2,
                            bg= 'white',
                            command=lambda: controller.show_frame("Yard"))


        button1.place(x=200, y=100)
        button2.place(x=200, y=200)
        button3.place(x=400, y=100)
        button4.place(x=400, y=200)



class Mainfloor(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="Main Floor View", font=controller.title_font)
        label.pack(side="top", fill="x", pady=10)

        button0 = tk.Button(self, text="Home Page", width=15,
                            font=controller.title_font2,
                            bg= 'white',
                           command=lambda: controller.show_frame("StartPage"))

        button1 = tk.Button(self, text="Main Floor", width=15,
                            font=controller.title_font2,
                            bg= 'black', fg= 'yellow',
                            command=lambda: controller.show_frame("Mainfloor"))

        button2 = tk.Button(self, text="Upstairs", width=15,
                            font=controller.title_font2,
                            bg= 'white',
                            command=lambda: controller.show_frame("Upstairs"))

        button3 = tk.Button(self, text="Basement", width=15,
                            font=controller.title_font2,
                            bg= 'white',
                            command=lambda: controller.show_frame("Basement"))

        button4 = tk.Button(self, text="Yard Lighting", width=15,
                            font=controller.title_font2,
                            bg= 'white',
                            command=lambda: controller.show_frame("Yard"))

##############fireplace button
        toggleButton1 = Button(self, text=("Fireplace"),
                               width=7, height=3, bg= 'white',
                               font=controller.title_font3,
                               relief="raised", command=Fireplace)

        button0.place(x=5, y=105)
        button1.place(x=5, y=135)
        button2.place(x=5, y=165)
        button3.place(x=5, y=195)
        button4.place(x=5, y=225)
        toggleButton1.place(x=355,y=140)

class Upstairs(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="Upstairs View", font=controller.title_font)
        label.pack(side="top", fill="x", pady=10)
        
        button0 = tk.Button(self, text="Home Page", width=15,
                            font=controller.title_font2,
                            bg= 'white',
                           command=lambda: controller.show_frame("StartPage"))

        button1 = tk.Button(self, text="Main Floor", width=15,
                            font=controller.title_font2,
                            bg= 'white',
                            command=lambda: controller.show_frame("Mainfloor"))

        button2 = tk.Button(self, text="Upstairs", width=15,
                            font=controller.title_font2,
                            bg= 'black', fg= 'yellow',
                            command=lambda: controller.show_frame("Upstairs"))

        button3 = tk.Button(self, text="Basement", width=15,
                            font=controller.title_font2,
                            bg= 'white',
                            command=lambda: controller.show_frame("Basement"))

        button4 = tk.Button(self, text="Yard Lighting", width=15,
                            font=controller.title_font2,
                            bg= 'white',
                            command=lambda: controller.show_frame("Yard"))


        button0.place(x=5, y=105)
        button1.place(x=5, y=135)
        button2.place(x=5, y=165)
        button3.place(x=5, y=195)
        button4.place(x=5, y=225)

class Basement(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller

        label = tk.Label(self, text="Basement View", font=controller.title_font)
        label.pack(side="top", fill="x", pady=10)
        
        button0 = tk.Button(self, text="Home Page", width=15,
                            font=controller.title_font2,
                            bg= 'white',
                           command=lambda: controller.show_frame("StartPage"))

        button1 = tk.Button(self, text="Main Floor", width=15,
                            font=controller.title_font2,
                            bg= 'white',
                            command=lambda: controller.show_frame("Mainfloor"))

        button2 = tk.Button(self, text="Upstairs",  width=15,
                            font=controller.title_font2,
                            bg= 'white',
                            command=lambda: controller.show_frame("Upstairs"))

        button3 = tk.Button(self, text="Basement", width=15,
                            font=controller.title_font2,
                            bg= 'black', fg= 'yellow',
                            command=lambda: controller.show_frame("Basement"))

        button4 = tk.Button(self, text="Yard Lighting", width=15,
                            font=controller.title_font2,
                            bg= 'white',
                            command=lambda: controller.show_frame("Yard"))

        button0.place(x=5, y=105)
        button1.place(x=5, y=135)
        button2.place(x=5, y=165)
        button3.place(x=5, y=195)
        button4.place(x=5, y=225)

class Yard(tk.Frame):
    
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        
        label = tk.Label(self, text="Yard View", font=controller.title_font)
        label.place(x=350, y=40)

        
        button0 = tk.Button(self, text="Home Page", width=15,
                            font=controller.title_font2,
                            bg= 'white',
                           command=lambda: controller.show_frame("StartPage"))

        button1 = tk.Button(self, text="Main Floor", width=15,
                            font=controller.title_font2,
                            bg= 'white',
                            command=lambda: controller.show_frame("Mainfloor"))

        button2 = tk.Button(self, text="Upstairs", width=15,
                            font=controller.title_font2,
                            bg= 'white',
                            command=lambda: controller.show_frame("Upstairs"))

        button3 = tk.Button(self, text="Basement", width=15,
                            font=controller.title_font2,
                            bg= 'white',
                            command=lambda: controller.show_frame("Basement"))

        button4 = tk.Button(self, text="Yard Lighting", width=15,
                            font=controller.title_font2,
                            bg= 'black', fg= 'yellow',
                            command=lambda: controller.show_frame("Yard"))


        button0.place(x=5, y=105)
        button1.place(x=5, y=135)
        button2.place(x=5, y=165)
        button3.place(x=5, y=195)
        button4.place(x=5, y=225)



if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()
Reply


Messages In This Thread
RE: Multi windows in tkinter buttons not responding correctly - by curtjohn86 - Jun-28-2017, 10:39 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  tkinter two windows instead of one jacksfrustration 7 962 Feb-08-2024, 06:18 PM
Last Post: deanhystad
  pass a variable between tkinter and toplevel windows janeik 10 2,523 Jan-24-2024, 06:44 AM
Last Post: Liliana
  Tkinter multiple windows in the same window tomro91 1 921 Oct-30-2023, 02:59 PM
Last Post: Larz60+
  tkinter toggle buttons not working Nu2Python 26 7,283 Jan-23-2022, 06:49 PM
Last Post: Nu2Python
  [PyGTK] How to center text on multi-line buttons? Lomax 3 4,305 Jan-23-2021, 03:23 PM
Last Post: Lomax
  Dual Tkinter windows and shells Astrikor 6 4,045 Sep-03-2020, 10:09 PM
Last Post: Astrikor
  [Tkinter] How to compare two variables correctly in tkinter scratchmyhead 2 3,930 May-10-2020, 08:04 PM
Last Post: scratchmyhead
  PyQt GUI not responding samuelbachorik 0 3,628 Apr-25-2020, 08:08 PM
Last Post: samuelbachorik
  TkInter Binding Buttons ifigazsi 5 4,609 Apr-06-2020, 08:30 AM
Last Post: ifigazsi
  Tkinter scaling windows conten to or with its size not working Detzi 5 4,554 Jan-12-2020, 12:42 PM
Last Post: Detzi

Forum Jump:

User Panel Messages

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