UITableViewCell spooky action
Tag : ios , By : Steve Jones
Date : November 22 2020, 10:31 AM
I wish this helpful for you dequeueReusableCellWithIdentifier: forIndexPath: gives you a cell, which might be a new cell, or it might be an old cell that's previously been shown, but has scrolled off the screen. One quick fix is: UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"forIndexPath:indexPath];
Produit *object = self.objects[indexPath.row];
[cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
[cell.contentView addSubview:object];
return cell;
|
How to call methods which are present in right side defined class. Python inheritance. multiple inheritance. Dimond scen
Date : March 29 2020, 07:55 AM
seems to work fine I have a class which is inherited from two other classes. When I call the methods from the child class, It always calls the method which mentioned left side in in class definition. , You can either call it directly: Second.get_details(child_obj)
Child.mro()[2].get_details(child_obj)
>>> Child.mro()
[__main__.Child, __main__.First, __main__.Second, object]
class Child(First, Second):
def get_details_second_1(self):
"""Option 1"""
return Second.get_details(self)
def get_details_second_2(self):
"""Option 2"""
return Child.mro()[2].get_details(self)
child_obj = Child()
child_obj.get_details_second_1()
child_obj.get_details_second_2()
|
Inheritance vs using a class object of one class as a field of another class (Python 3)
Date : March 29 2020, 07:55 AM
seems to work fine Say we have a product which is identifiable by an ID number and a Quality field which is in turn comprised of several values, one of which is 2d dimensions values. Is this a case of inheritance or just using an object as another classes' field? , Is what I am trying to do above achieved via inheritance? class Dimensions:
def __init__(self, x, y):
self.x = x
self.y = y
class Quality:
def __init__(self, val1, val2, dimensions):
self.val1 = val1
self.val2 = val2
self.dimensions = dimensions
class Product:
def __init__(self, id, quality):
self.id = id
self.quality = quality
dimensions = Dimensions(1, 2)
quality = Quality("A", "B", dimensions)
product = Product(42, quality)
class Dimensions:
def __init__(self, x, y):
self.x = x
self.y = y
class Quality:
def __init__(self, val1, val2, x, y):
self.val1 = val1
self.val2 = val2
self.dimensions = Dimensions(x, y)
class Product:
def __init__(self, id, val1, val2, x, y):
self.id = id
self.quality = Quality(val1, val2, x, y)
product = Product(42, "A", "B", 1, 2)
|
Is function inheritance possible like class inheritance in python?
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further As mentioned in the comments, you have it setup to only play the alarm iff the user clicks the button at the time the alarm is set to. Instead, you should check the time every second, and play the alarm at the given time: def alarm_time():
hrs = int(hrs_opt_ctrl.get())
min = int(min_opt_ctrl.get())
tme = tme_ctrl.get()
if hrs == int(time.strftime('%I')) and min == int(time.strftime('%M')) and tme == time.strftime('%p'):
# Time is up. Play the sound
alarm_ringtone = pygame.mixer.music.load('alarm_noise.mp3')
pygame.mixer.music.play()
# Don't call after again
else:
# Not time, sleep for 1 second
window.after(1000, alarm_time)
from tkinter import *
import pygame
import time
pygame.mixer.init()
window = Tk()
window.geometry('300x200')
window.title('Alarm')
#logo = PhotoImage(file='alarm.gif')
lab_1 = Label(window, text='Alarm Clock', font=('Times',20,'bold')).grid(column=200, row=0)
#lab_2=Label(window,image=logo).grid(column=300,row=0,columnspan=3)
lab_3 = Label(window, text='Hours', font=('Comic',10,'bold')).grid(column=50,row=10, columnspan=3)
lab_4 = Label(window, text='Minutes', font=('Comic',10,'bold')).grid(column=90,row=10, columnspan=3)
# Alarm class
class Alarm:
alarm_id = 1
def __init__(self, hours, minutes, ampm, sound_file):
self.sound_file = sound_file
# Convert hours, minutes, ampm to a timestamp
# Save time as a timestamp
t = time.localtime()
t = time.strptime(f"{t.tm_year}-{t.tm_mon}-{t.tm_mday} {hours} {minutes} {ampm}", "%Y-%m-%d %I %M %p")
self.alarm_time = time.mktime(t)
# Number of seconds to snooze
self.snooze_time = None
self.completed = False # Set to True after alarm has gone off
self.id = Alarm.alarm_id
Alarm.alarm_id += 1
# Every time this is called, it checks the time to see if the alarm should go off
def check_time(self, cur_time):
# Use alarm time or snooze time?
on_time = self.snooze_time if self.snooze_time else self.alarm_time
# Since might not be called when seconds is 0, check if it is with one minute of alarm time
time_diff = cur_time - on_time
if not self.completed and time_diff >= 0 and time_diff < 60:
self.completed = True
alarm_ringtone = pygame.mixer.music.load(self.sound_file)
pygame.mixer.music.play()
# Reset after 30 minutes - add 24 hours (daily timer)
elif self.completed and time_diff > 1800 and time_diff < 1860:
self.completed = False
self.snooze_time = None
self.alarm_time += 24 * 60 * 60
def snooze(self, minutes):
if self.completed and pygame.mixer.music.get_busy():
self.snooze_time = time.time() + (minutes * 60)
self.completed = False
pygame.mixer.music.stop()
# Convert to string for printing
def __str__(self):
id_str = f"[{self.id}]: "
if self.completed:
return id_str + ("Alarm in progress" if pygame.mixer.music.get_busy() else "Alarm completed")
elif self.snooze_time:
time_left = int(self.snooze_time - time.time())
minutes = time_left // 60
seconds = time_left % 60
if minutes:
return id_str + f"Snoozing for {minutes} minutes and {seconds} seconds"
else:
return id_str + f"Snoozing for {seconds} seconds"
else:
return id_str + f"Alarm set for {time.ctime(self.alarm_time)}"
# This list holds all alarms
all_alarms = []
# Tell all alarms to check the time
def check_alarms():
now = time.time()
for alarm in all_alarms:
print(f"Checking: {alarm}");
alarm.check_time(now)
# Call again after 1 second
window.after(1000, check_alarms)
# Creates a single object of the Alarm class
# Uses values from the option controls
def create_alarm():
hours = int(hrs_opt_ctrl.get())
minutes = int(min_opt_ctrl.get())
ampm = tme_ctrl.get()
alarm = Alarm(hours, minutes, ampm, "alarm.mp3")
all_alarms.append(alarm)
print(f"Adding: {alarm}");
# Snoozes all active alarms for 2 minutes
def snooze_current():
for alarm in all_alarms:
alarm.snooze(2)
but = Button(window, text='Set Alarm', font=('Comic',10,'bold'), command=create_alarm).grid(column=100,row=15)
snooze = Button(window, text='Snooze', font=('Comic',10,'bold'), command=snooze_current).grid(column=100,row=16)
opt_hrs = []
opt_min = []
opt_tme = ('AM','PM')
for i in range(1,13):
opt_hrs.append(i)
for j in range(0,60):
opt_min.append(j)
hrs_opt_ctrl = StringVar()
min_opt_ctrl = StringVar()
tme_ctrl = StringVar()
hrs_lab = OptionMenu(window, hrs_opt_ctrl, *opt_hrs).grid(column=70,row=10,columnspan=3)
min_lab = OptionMenu(window, min_opt_ctrl, *opt_min).grid(column=100,row=10,columnspan=3)
tme_lab = OptionMenu(window, tme_ctrl, *opt_tme).grid(column=120,row=10,columnspan=3)
# Fill with default values of current time
hrs_opt_ctrl.set(str(int(time.strftime('%I'))))
min_opt_ctrl.set(str(int(time.strftime('%M'))))
tme_ctrl.set(time.strftime('%p'))
check_alarms()
window.mainloop()
pygame.mixer.music.stop()
|
Python inheritance - calling base class methods inside child class?
Tag : python , By : user152319
Date : March 29 2020, 07:55 AM
|