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
#1
My apologies I am new to forums and new to coding, I am sure I'm doing everything completely backwards.

I am trying to use my RPI 3b for home automation. I am using Python 3 and tkinter as a GUI.

I have reduced my program drastically to try and assist in easier troubleshooting, currently only showing different page and only one button under "main level" page.

Recently I only had one tkinter window with many buttons to toggle pins on my IO pi plus, at this point everything worked well.

Just this week I switched over to multiple pages to give it a smoother appearance.

The problem I am encountering currently is my GUI buttons do not show indication of the pins state of which they are toggling, on or off.

The error I get is " ToggleButton1["text"] = ("Fireplace On")
NameError: name 'toggleButton1' is not defined "

I am assuming this has something to do with classes (which I'm currently trying to comprehend) or even placement.

FYI if you try to run program you will need to change to gpio rather than looking for i2c bus.

Thank you in advance for any insight you may have resolve my issue

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
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
#2
I'm finding it hard to follow your code because there is no indentation within it as it's displayed here.
So it may be that when you are doing
1
ToggleButton1["text"] = ("Fireplace On")
It cannot see where you have defined ToggleButton1
also I think there is a syntax error anyway. I think it should be
1
ToggleButton1["text"] = "Fireplace On"
I'm a long way from being an expert but it seems there are many errors in the code. for example in a case where you use def
in this part of the code I have shown the indentation I would expect
1
2
3
4
5
6
7
8
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")
but I have no idea if the
1
2
3
4
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")
part should be indented to be part of the class itself.
No doubt someone who is far more conversant with this will help.
Reply
#3
Sorry, for some reason when I copied and pasted I lost all indentations. I will try to edit it.
Reply
#4
This should be the correct indentation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
    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
#5
My word, Please fix your indentation. You will get a more responses to your questions
Reply
#6
Can you post the full error message rather than what you think is the important part please?
Reply
#7
(Jun-29-2017, 09:42 PM)Barrowman Wrote: Can you post the full error message rather than what you think is the important part please?

Barrowman this is the full error I am receiving, sorry i did not realize I missed some of it.

1
2
3
4
5
6
7
Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python3.4/tkinter/__init__.py", line 1536, in __call__
    return self.func(*args)
  File "/home/pi/Desktop/Programs is Use/multi window attempt.py", line 77, in __init__
    toggleButton1["text"] = ("Fireplace On")
NameError: name 'toggleButton1' is not defined
(Jun-29-2017, 01:20 AM)Larz60+ Wrote: My word, Please fix your indentation. You will get a more responses to your questions

Larz60 I posted the correct indentation above your reply

(Jun-28-2017, 10:39 PM)curtjohn86 Wrote: This should be the correct indentation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
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
#8
I haven't been using classes myself but looking at this and other ( working ) examples of code I think you need a change:
1
2
3
4
5
6
7
8
9
10
11
12
13
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")
     
   
to this:
1
2
3
4
5
6
7
8
9
10
 class Fireplace@
   def __init__(self):
 
       if (bus1.read_pin(15) == 1):
          bus1.write_pin(15, 0)
          Mainfloor.toggleButton1["text"] = ("Fireplace On")
         
       else:Mainfloor
               bus1.write_pin(15, 1)
               Mainfloor.toggleButton1["text"] = ("Fireplace Off")
Of course I may be wrong as I usually am  Confused
Reply
#9
(Jun-30-2017, 06:07 PM)Barrowman Wrote: I haven't been using classes myself but looking at this and other ( working ) examples of code I think you need a change:

[snip]

to this:
1
2
3
4
5
6
7
8
9
10
 class Fireplace@
   def __init__(self):
 
       if (bus1.read_pin(15) == 1):
          bus1.write_pin(15, 0)
          Mainfloor.toggleButton1["text"] = ("Fireplace On")
         
       else:Mainfloor
               bus1.write_pin(15, 1)
               Mainfloor.toggleButton1["text"] = ("Fireplace Off")
Of course I may be wrong as I usually am  Confused

Quote:
1
 class Fireplace@
The class keyword starts a definition block, and like all other block structures (def, if, for), it uses a colon at the end of the line, not an @.

Quote:
1
Mainfloor.toggleButton1["text"] = ("Fireplace On")
The Mainfloor class, itself, does not have a toggleButton1, but it's instances do. So that would throw an error as you're trying to modify something that doesn't exist.

Nice try, though :)
Reply
#10
toggleButton1 only exists in the MainFloor class.  You're trying to change it from within the Fireplace class.  A nice way to handle this would be using events or signals (something frequently done in game programming, and is how something like the physics code will do most of it's communication).  But that'd be a whole lot of changes.

You currently pass each object a controller object, but the Fireplace doesn't keep track of it.  If you do, then you can crawl into the controller and manipulate it's frames to get to the MainFloor to do what you're trying to do.  So, Fireplace would then look like this:

1
2
3
4
5
6
7
8
9
10
11
class Fireplace():
   def __init__(self, controller):
       self.controller = controller
       main_floor = self.controller.frames["MainFloor"]
 
       if (bus1.read_pin(15) == 1):
           bus1.write_pin(15, 0)
           main_floor.toggleButton1["text"] = ("Fireplace On")
       else:
           bus1.write_pin(15, 1)
           main_floor.toggleButton1["text"] = ("Fireplace Off")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Tkinter & Application flow issues - using multi-threaded for GUI caarsonr 4 2,012 Jan-06-2025, 01:35 AM
Last Post: deanhystad
Smile Unable to display Devanagari ligatures correctly in python tkinter Anamika 4 1,382 Nov-26-2024, 08:08 PM
Last Post: woooee
  Tkinter multiple windows in the same window hosierycouch 1 1,233 May-30-2024, 04:28 AM
Last Post: deanhystad
  tkinter two windows instead of one jacksfrustration 7 2,506 Feb-08-2024, 06:18 PM
Last Post: deanhystad
  Tkinter multiple windows in the same window tomro91 1 2,171 Oct-30-2023, 02:59 PM
Last Post: Larz60+
  pass a variable between tkinter and toplevel windows janeik 9 6,181 Oct-05-2023, 04:22 AM
Last Post: janeik
  tkinter toggle buttons not working Nu2Python 26 12,135 Jan-23-2022, 06:49 PM
Last Post: Nu2Python
  [PyGTK] How to center text on multi-line buttons? Lomax 3 5,848 Jan-23-2021, 03:23 PM
Last Post: Lomax
  Dual Tkinter windows and shells Astrikor 6 6,471 Sep-03-2020, 10:09 PM
Last Post: Astrikor
  [Tkinter] How to compare two variables correctly in tkinter scratchmyhead 2 4,752 May-10-2020, 08:04 PM
Last Post: scratchmyhead

Forum Jump:

User Panel Messages

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