Reputation: 23
could you please lead me to a solution, I can't fix this on my own.
I get an AttributeError: 'mainscreen' object has no attribute 'attributes' when I press the button to toggle to fullscreen. thx for your help
#!/bin/python3
import tkinter
import grovepi
import math
import time
from tkinter import Tk, Label, Button
class mainscreen:
def __init__(self,master):
self.master = master
master.title ("Temperature")
master.geometry("800x480")
master.attributes("-fullscreen", False)
self.state = False
sensorTH1 = 4
sensorTH2 = 3
temp1=0
temp2=0
#column0 and 1
self.labeltemp11 = tkinter.Label (master, text="Temp inside", fg="black", font="72")
self.labeltemp11.grid(row=1, column=0 ,sticky="sw")
self.labeltemp1 =tkinter.Label (master, text="", fg="black", font="72")
self.labeltemp1.grid(row=1, column=1,sticky="sw")
self.labelhum11 = tkinter.Label(master, text="Hum inside", fg="black", font="72")
self.labelhum11.grid(row=2,column=0,sticky="sw")
self.labelhum1 = tkinter.Label (master, text="",fg="black", font="72")
self.labelhum1.grid(row=2, column=1,sticky="sw")
self.labeltemp22 = tkinter.Label (master, text="Temp outside", fg="black",font="72")
self.labeltemp22.grid(row=3, column=0,sticky="sw")
self.labeltemp2 = tkinter.Label (master, text="", fg="black", font="72")
self.labeltemp2.grid(row=3, column=1,sticky="sw")
self.labelhum22 = tkinter.Label(master, text="Hum outside", fg="black", font="72")
self.labelhum22.grid(row=4,column=0,sticky="sw")
self.labelhum2 = tkinter.Label (master, text="", fg="black", font="72")
self.labelhum2.grid(row=4, column=1,sticky="sw")
#win.focus_set()
#win.bind("t",toggle_fullscreen(win))
self.button= tkinter.Button(master, text="No fullscreen", width=10,command=lambda:self.toggle_fullscreen())
#works as well button= tkinter.Button(win, text="No Fullscreen", width=10,command=toggle_fullscreen)
self.button.grid(column=0,row=5,sticky="nsew")
#column2 empty
self.labelemp1 = tkinter.Label (master, text="", fg="black", width="10" ,bg="white" , font="72")
self.labelemp1.grid(row=0, column=2,sticky="sw")
self.labelemp2 = tkinter.Label (master, text="test", fg="black", width="10",bg="black" , font="72")
self.labelemp2.grid(row=0, column=3,sticky="sw")
def READ(toggle):
if toggle:
[temp1,humidity1] = grovepi.dht(sensorTH1,1)
self.labeltemp1.configure(text= "{value:6.2f} C".format( value=temp1))
self.labelhum1.configure(text= "{value:6.2f} %".format( value=humidity1))
print ("temp1 =", temp1, " humidity1 =", humidity1) #py3
else:
#time.sleep(1)
[temp2,humidity2] = grovepi.dht(sensorTH2,1)
self.labeltemp2.configure(text= "{value:6.2f} C".format( value=temp2))
self.labelhum2.configure(text= "{value:6.2f} %".format( value=humidity2))
print ("temp2 =", temp2, " humidity2 =", humidity2) #py3
toggle= not toggle
#print (toggle)
return(toggle)
def read_every_second(self):
#global readoneortwo not needed after object oriented programming
#win.attributes("-fullscreen", False)
#toggle_fullscreen(win)
readoneortwo=self.READ(readoneortwo)
master.after(1000, read_every_second)
# can be used with call of button lamda: toggle_fullscreen (win) or any other window?
#def toggle_fullscreen(self, event=None):
# self.state = not self.state # Just toggling the boolean
# print ("test")
# self.attributes("-fullscreen", self.state)
# return "break"
def toggle_fullscreen(self, event=None):
self.state = not self.state # Just toggling the boolean
if self.state:
self.button["text"]="No Fullscreen"
else:
self.button["text"]="Fullscreen"
self.attributes("-fullscreen", master.state)
return "break"
if __name__ == '__main__':
root = tkinter.Tk()
win = mainscreen(root)
#win.read_every_second ()
root.mainloop()
If I want to run win.read_every_second() I get an Error:
UnboundLocalError: local variable 'readoneortwo' referenced before assignment
I was told to use object oriented and class so I do not need global variables , but it seems I do ??
Upvotes: 0
Views: 906
Reputation:
To solve this issue change toggle_fullscreen
as follows:
def toggle_fullscreen(self, event=None):
self.state = not self.state # Just toggling the boolean
if self.state:
self.button["text"]="No Fullscreen"
else:
self.button["text"]="Fullscreen"
self.master.attributes("-fullscreen", self.state)
return "break"
And notice the change to self.state
( from master.state
).
You write: I was told to use object oriented and class so I do not need global variables , but it seems I do??
Using class you don't need global variables as long as you use the syntax
self.someVariable = someValue
. Every function of the class getsself
passed as its first parameter making all theself.someVariable
variables defined within the class available from within the function (as if they were "global").
Upvotes: 2