Kivy Infinite Looping ScrollView - python-3.x

I am trying to create a ScrollView in Kivy with widgets that loop infinitely like in this demo I found online:
I have tried to remove widgets outside the ScrollView's bounds and add them back to the top or bottom, depending on their original location. However, this made the whole app flicker and glitch and ultimately did not work. If anyone has a method of achieving this, it would be greatly appreciated!

I created a custom class based on kivy's Stencilview and RelativeLayout.
It's used pretty much like kivy.uix.recycleview and supports looping. It's somewhat unoptimized but works pretty well. No x-axis scrolling (yet). Should be capable of processing an infinite amount of data without any performance loss. (Tested with 10.000.000 data entries => no lag)).
please don't quote me on anything inside on_touch_down, on_touch_move, on_touch_up, or any function called inside it (the only exception is scroll_y) cause I have no idea how they work.
from functools import partial
from kivy.app import App
from kivy.clock import Clock
from kivy.compat import iteritems
from kivy.lang import Builder
from kivy.metrics import sp
from kivy.properties import DictProperty, ListProperty, NumericProperty, BooleanProperty, ObjectProperty
from kivy.uix.label import Label
from kivy.uix.relativelayout import RelativeLayout
from kivy.uix.stencilview import StencilView
from kivy.uix.widget import Widget
class LoopEntry(Widget):
data_index = NumericProperty(0)
data = DictProperty(None, allow_none=True)
hidden = BooleanProperty(False)
def is_hidden(self):
"""
:return:
"""
return self.hidden
def hide(self):
"""
:return:
"""
self.opacity = 0
self.hidden = True
def show(self):
"""
:return:
"""
self.opacity = 1
self.hidden = False
def update(self, data):
"""
overwrite this function if values other than attributes are needed
:param data:
:return:
"""
assert isinstance(data, dict)
# assign data
self.data = data
# apply values
for key, value in iteritems(data):
setattr(self, key, value)
class LoopContainer(RelativeLayout, StencilView):
pass
class LoopContainerDebug(RelativeLayout):
pass
class LoopScrollView(RelativeLayout):
"""
Main data source. Contains the data that needs to be exchanged.
Data can be manipulated without a whole redraw. Might cause unwanted behaviour
like blank lines and incorrect orders if not properly updated.
Setting this value causes a complete refresh.
"""
data = ListProperty()
"""
Children height. All children need to be the same height else
unwanted behaviour might occur.
Altering this value causes a complete refresh.
"""
children_height = NumericProperty(44)
"""
Amount of widgets added to the minimum widgets. (Very) Big numbers may cause lag.
Altering this value causes a complete refresh
"""
protection_amount = NumericProperty(4)
"""
viewclass is used to set the class type the widgets should be
future version might support manual adding of widgets
Altering this value causes a complete refresh.
"""
viewclass = ObjectProperty(LoopEntry)
"""
controls looping behaviour.
"""
loop = BooleanProperty(True)
"""
debugging option. shows hidden entries. not possible to switch
while running (yet)
"""
debug = BooleanProperty(False)
"""
scroll timeout. If the mouse has not been moved 'scroll_distance' within this time, dispatch the touch to children
in milliseconds
"""
scroll_timeout = NumericProperty(200)
"""
touch distance. Distance mouse needs to be moved
in pixel
"""
scroll_distance = NumericProperty('20dp')
def __init__(self, **kwargs):
"""
:param kwargs:
"""
# minimum widgets controls min/max amount of widgets on screen. Readonly.
self.__minimum_widgets = 0
# controls overscroll blocking if loop is disabled
self.__overscroll_block_y = "free"
# container
_kwargs = {
"size_hint": (None, None),
"size": (0, 0)
}
self.container = LoopContainer(**_kwargs) if not kwargs.get('debug', False) else LoopContainerDebug(**_kwargs)
# init super values
super(LoopScrollView, self).__init__(**kwargs)
# add container
self.add_widget(self.container)
# create widgets
self.__create_widgets()
# no idea
self._drag_touch = None
def on_pos(self, widget, value) -> None:
"""
:param widget:
:param value:
:return:
"""
def on_size(self, widget, value) -> None:
"""
:param widget:
:param value:
:return:
"""
# set container size
self.container.size = self.size
# recreate widgets
self.__create_widgets()
def on_data(self, widget, value) -> None:
"""
called if new data is set. Forces complete refresh. Use with care.
:param widget: widget event belongs to
:param value: event value
:return: None
"""
self.__create_widgets()
def on_protection_amount(self, widget, value) -> None:
"""
Forces complete refresh. Use with care.
:param widget: widget event belongs to
:param value: event value
:return: None
"""
self.__create_widgets()
def on_viewclass(self, widget, value) -> None:
"""
sets the viewclass used to entries
Forces complete refresh
:param widget: widget
:param value: value
:return: None
"""
self.__create_widgets()
def on_children_height(self, widget, value) -> None:
"""
Changes the children height.
Forces complete refresh.
:param widget: widget
:param value: value
:return: None
"""
self.__create_widgets()
def __create_widgets(self) -> None:
"""
clear all widgets and recreate
:return: None
"""
# remove all widgets
self.container.clear_widgets()
# calculate the minimum amount of required widgets
self.minimum_widgets = round(self.height / self.children_height) + self.protection_amount
# adding entries to the stencil view in reversed order to start with the smallest value (index 0) at top
for entry in range(self.minimum_widgets, 0, -1):
# create widget instance
_tmp_entry = self.viewclass(
size_hint=(1, None),
height=self.children_height,
pos=(0, self.height - self.children_height * entry)
)
# add to container
self.container.add_widget(_tmp_entry)
# refresh all widgets from given index and apply data values
self.__refresh_from_index(0)
def __refresh_from_index(self, index=0) -> None:
"""
refreshes widgets from given index
:param index: index to start with (very top entry)
:return: None
"""
# return if data is empty
if not self.data:
return
# reset widget positions to prevent weird behaviour
self.__reset_widget_positions(brute=True)
# reduce overhead. Slightly.
_data_length = len(self.data)
# loop through children and set values from given index
for child in self.container.children:
# if the current index exceeds the lengths and looping is disabled hide the widget
if index >= _data_length and not self.loop:
# Note : I dislike direct changing of values -_-
if not child.is_hidden():
child.hide() # hide child
child.data_index = index
else:
_normalized_index = index % _data_length
# get the new data value for the widget
_data_value = self.data[_normalized_index]
child.update(_data_value)
child.data_index = _normalized_index
if child.is_hidden():
child.show()
# increase index
index += 1
def __reset_widget_positions(self, brute=False) -> None:
"""
resets widget positions. Does not take into account values or
value positions.
If brute is True positions will be reset forcefully meaning data may mix up.
If brute is False positions will be scrolled meaning values will remain ordered.
:param brute: boolean
:return: None
"""
if brute:
# forcefully reset children
for child in self.container.children:
child.y = self.height - (child.height * (self.get_child_index(child) + 1))
else:
# get top child
_top_child = self.container.children[0]
# loop until child's y value matches the top threshold
while _top_child.y != self.height - _top_child.height:
# scroll by up to ensure proper order
self.scroll_y(1)
def __trigger_overscroll(self, entry: (LoopEntry, None), state):
"""
:param entry:
:param state:
:return:
"""
# trigger overscroll for down
if state == "bottom" and entry is not None:
# reset child to a proper spot
while entry.y != 0:
# scroll in the fastest direction
self.scroll_y(1 if entry.y < 0 else -1)
# set overscroll AFTER scrolling
self.__overscroll_block_y = "bottom"
# for up
elif state == "top" and entry is not None:
# reset child to proper spot
while entry.y != self.height - entry.height:
self.scroll_y(1 if entry.y < self.height - entry.height else 1)
# set overscroll AFTER scrolling
self.__overscroll_block_y = "top"
# reset else
else:
# free scrolling
self.__overscroll_block_y = "free"
def __update_entry(self, entry: LoopEntry, direction) -> None:
"""
:param entry:
:param direction:
:return:
"""
# get data length
_data_length = len(self.data)
# check direction
if direction == "down":
# get new index
_data_index = entry.data_index + self.minimum_widgets
if self.loop:
# normalize data index
_normalized_data_index = _data_index % _data_length
# update entry
entry.update(self.data[_normalized_data_index])
# set data index
entry.data_index = _normalized_data_index
# show entry
if entry.is_hidden():
entry.show()
else:
# if loop is disabled and data index exceeds either direction
if _data_index >= _data_length or _data_index < 0:
# hide children
if not entry.is_hidden():
entry.hide()
else:
# update entry from data index
entry.update(self.data[_data_index])
# show entry
if entry.is_hidden():
entry.show()
# set data index
entry.data_index = _data_index
elif direction == "up":
# get new data index
_data_index = entry.data_index - self.minimum_widgets
# if looping is enabled
if self.loop:
# normalize index
_normalized_data_index = _data_index % _data_length
# update entry
entry.update(self.data[_normalized_data_index])
# set data index
entry.data_index = _normalized_data_index
# show entry
if entry.is_hidden():
entry.show()
else:
if _data_index < 0 or _data_index >= _data_length:
# hide children
if not entry.is_hidden():
entry.hide()
else:
# update entry from data index
entry.update(self.data[_data_index])
# show entry
if entry.is_hidden():
entry.show()
# set data index
entry.data_index = _data_index
else:
# error
raise Exception
def get_child_index(self, child) -> (int, None):
"""
returns the index if the child exists in list else None
:param child: child instance
:return: int,None
"""
return self.container.children.index(child) if child in self.container.children else None
def scroll_y(self, delta_y) -> None:
"""
scroll by given amount
:param delta_y: delta value in pixels
:return: None
"""
# set highest and lowest children (needed for rotation)
_highest, _lowest = self.container.children[0], self.container.children[0]
# round delta y
delta_y = round(delta_y)
# get data length
data_length = len(self.data)
# control var
_free_block = True
# loop through children
for child in self.container.children:
# increase/decrease children y position
child.y += delta_y
# update highest and lowest children
_highest = child if child.y > _highest.y else _highest
_lowest = child if child.y < _lowest.y else _lowest
# check for loop condition
if not self.loop:
# if current child's index exceeds or evens data length and is higher than given
# threshold trigger overscroll event for bottom
if child.data_index >= data_length - 1 and child.y >= 0:
self.__trigger_overscroll(child, 'bottom')
_free_block = False
# if current child's index is smaller or evens 0 and is higher than the given
# threshold trigger overscroll event for top
elif child.data_index <= 0 and child.y <= self.height - child.height:
self.__trigger_overscroll(child, 'top')
_free_block = False
# unblock if block is free to be unblocked
if _free_block:
self.__trigger_overscroll(None, 'reset')
# check if swap is needed
if _highest.y > self.height + _highest.height and delta_y > 0:
# set new y for highest if it exceeds max height
_highest.y = _lowest.y - _highest.height
self.__update_entry(_highest, direction="down")
elif _lowest.y < 0 - _lowest.height - _lowest.height and delta_y < 0:
_lowest.y = _highest.y + _highest.height
self.__update_entry(_lowest, direction="up")
def _get_uid(self, prefix='sv'):
return '{0}.{1}'.format(prefix, self.uid)
def on_touch_down(self, touch):
x, y = touch.pos
if not self.collide_point(x, y):
touch.ud[self._get_uid('svavoid')] = True
return super(LoopScrollView, self).on_touch_down(touch)
if self._drag_touch or ('button' in touch.profile and touch.button.startswith('scroll')):
return super(LoopScrollView, self).on_touch_down(touch)
# no mouse scrolling, so the user is going to drag with this touch.
self._drag_touch = touch
uid = self._get_uid()
touch.grab(self)
touch.ud[uid] = {
'mode': 'unknown',
'dx': 0,
'dy': 0
}
Clock.schedule_once(self._change_touch_mode, self.scroll_timeout / 1000.)
return True
def on_touch_move(self, touch):
if self._get_uid('svavoid') in touch.ud or self._drag_touch is not touch:
return super(LoopScrollView, self).on_touch_move(touch) or self._get_uid() in touch.ud
if touch.grab_current is not self:
return True
uid = self._get_uid()
ud = touch.ud[uid]
mode = ud['mode']
if mode == 'unknown':
ud['dx'] += abs(touch.dx)
ud['dy'] += abs(touch.dy)
if ud['dx'] > sp(self.scroll_distance):
mode = 'drag'
if ud['dy'] > sp(self.scroll_distance):
mode = 'drag'
ud['mode'] = mode
if mode == 'drag':
if (touch.dy > 0 and self.__overscroll_block_y == "bottom" or
touch.dy < 0 and self.__overscroll_block_y == "top"):
pass
else:
self.scroll_y(touch.dy)
return True
def on_touch_up(self, touch):
if self._get_uid('svavoid') in touch.ud:
return super(LoopScrollView, self).on_touch_up(touch)
if self._drag_touch and self in [x() for x in touch.grab_list]:
touch.ungrab(self)
self._drag_touch = None
ud = touch.ud[self._get_uid()]
if ud['mode'] == 'unknown':
super(LoopScrollView, self).on_touch_down(touch)
Clock.schedule_once(partial(self._do_touch_up, touch), .1)
else:
if self._drag_touch is not touch:
super(LoopScrollView, self).on_touch_up(touch)
return self._get_uid() in touch.ud
def _do_touch_up(self, touch, *largs):
super(LoopScrollView, self).on_touch_up(touch)
# don't forget about grab event!
for x in touch.grab_list[:]:
touch.grab_list.remove(x)
x = x()
if not x:
continue
touch.grab_current = x
super(LoopScrollView, self).on_touch_up(touch)
touch.grab_current = None
def _change_touch_mode(self, *largs):
if not self._drag_touch:
return
uid = self._get_uid()
touch = self._drag_touch
ud = touch.ud[uid]
if ud['mode'] != 'unknown':
return
touch.ungrab(self)
self._drag_touch = None
touch.push()
touch.apply_transform_2d(self.parent.to_widget)
super(LoopScrollView, self).on_touch_down(touch)
touch.pop()
return
# ------------------ Showcase ------------------ #
from kivy.uix.button import Button
class LoopLabel(LoopEntry, Label):
pass
class LoopButton(LoopEntry, Button):
pass
__style = ("""
<LoopLabel>:
color : 1,1,1
text: "test"
canvas:
Color:
rgb : 1,1,1
Line:
rectangle: (*self.pos,self.width ,self.height )
<LoopContainer,LoopContainerDebug>:
canvas:
Color:
rgb : 1,0,0
Line:
rectangle: (0+1,0+1,self.width - 1,self.height -1 )
width:5
""")
class InfiniteScrollingScrollView(App):
def build(self):
root = RelativeLayout()
root.bind(on_touch_down=lambda x, y: print("-" * 10))
sv = LoopScrollView(
size_hint=(0.5, 0.5), pos_hint={'center': (0.5, 0.5)}, viewclass=LoopLabel, debug=False
)
sv.data = [{'text': str(x)} for x in range(10000000)]
root.add_widget(sv)
return root
if __name__ == "__main__":
Builder.load_string(__style)
InfiniteScrollingScrollView().run()

i think this helpful but not the Answer
and this will help https://youtu.be/l8Imtec4ReQ in minute 59:30
from kivy.app import App
from kivy.uix.label import Label
from kivy.lang.builder import Builder
kv=Builder.load_string('''
ScrollView:
do_scroll_x:False
scroll_type:['bars', 'content']
bar_width:'20dp'
GridLayout:
id:g
cols:1
size_hint:(1,None)
height:self.minimum_height
''')
class a(App):
def build(self):
return kv
def on_start(self):
for i in range(100):self.root.ids.g.add_widget(Label(text=str(i),size_hint=(1, None),size=(1, 20)))
a().run()

Related

QHeaderView going out of viewport

I'm having couple of issues while trying to make a QHeaderView for my QTableView.
I want QHeaderView to be resizable by the user (Qt.ResizeMode.Interactive) while being able to stretch its sections proportionately when the window or QTableView is being resized. I found this problem online, and managed to mostly solve it but there is still some stuttering when the resizing begins and I think there should be a better solution than mine. Currently it's done by using QTimer to stop sections from going out of the viewport. Timer is being updated every millisecond. If the update interval is bigger, sections would go out of viewport and magically teleport back when the timer is updated, so once per millisecond in my case. There's still some stuttering visible if the user is dragging sections out of the viewport by dragging their mouse faster, not so visible when the mouse is slower, but visible none the less.
Every section should be resizable and movable, besides the first two. The first two sections should be immovable and fixed. I managed to make them fixed and they don't seem to have an effect on resizing of the sections, but I have no idea how to make them immovable while all the other sections are movable.
Sections should have text eliding, which I managed to make an item delegate for, but setting it on QHeaderView seems to do absolutely nothing (paint() method doesn't even get called). It's probably because item delegate isn't affecting sections, if so, how can I make a delegate that does affect them?
Here's my current code (it's a bit of a mess, but hopefully you'll get the idea):
import sys
import weakref
from typing import Any, Optional
from PyQt6 import QtWidgets, QtCore, QtGui
from PyQt6.QtCore import pyqtSlot, Qt
from PyQt6.QtGui import QFontMetrics
from PyQt6.QtWidgets import QHeaderView, QStyledItemDelegate, QStyleOptionViewItem
class MyItemDelegate(QStyledItemDelegate):
def __init__(self, parent=None):
super().__init__(parent)
def paint(self, painter: QtGui.QPainter, option: QStyleOptionViewItem, index: QtCore.QModelIndex) -> None:
text = index.data(Qt.ItemDataRole.DisplayRole)
# print(text)
if text:
elided_text = QFontMetrics(option.font).elidedText(str(text), Qt.TextElideMode.ElideRight, option.rect.width())
painter.drawText(option.rect, Qt.AlignmentFlag.AlignLeft, elided_text)
class HeaderView(QtWidgets.QHeaderView):
def __init__(self,
orientation: QtCore.Qt.Orientation = Qt.Orientation.Horizontal,
parent: Optional[QtWidgets.QWidget] = None):
super(HeaderView, self).__init__(orientation, parent)
item_delegate = MyItemDelegate(self)
self.setItemDelegate(item_delegate)
self.setMinimumSectionSize(5)
self.setStretchLastSection(True)
self.setCascadingSectionResizes(True)
self.setSectionsMovable(True)
self.fixed_section_indexes = (0, 1)
timer = QtCore.QTimer(self)
timer.setSingleShot(True)
timer.setTimerType(Qt.TimerType.PreciseTimer)
timer.timeout.connect(self._update_sizes)
resize_mode_timer = QtCore.QTimer(self)
resize_mode_timer.setTimerType(Qt.TimerType.PreciseTimer)
resize_mode_timer.setSingleShot(True)
resize_mode_timer.timeout.connect(lambda: self.setSectionResizeMode(QHeaderView.ResizeMode.Interactive))
self._resize_mode_timer = weakref.proxy(resize_mode_timer)
self._timer = weakref.proxy(timer)
self.sectionResized.connect(self._handle_resize)
self.setTextElideMode(Qt.TextElideMode.ElideLeft)
self.setDefaultAlignment(Qt.AlignmentFlag.AlignLeft)
self.proportions = []
self.mouse_pressed = False
def mouseReleaseEvent(self, e: QtGui.QMouseEvent) -> None:
self.mouse_pressed = False
super().mouseReleaseEvent(e)
self.proportions = [self.sectionSize(i) / self.width() for i in range(self.count())]
# print(self.mouse_pressed)
def init_sizes(self):
each = self.width() // self.count()
for i in range(self.count()):
self.resizeSection(self.logicalIndex(i), each)
#pyqtSlot(int, int, int)
def _handle_resize(self, logicalIndex: int, oldSize: int, newSize: int):
self._timer.start(1)
def resizeEvent(self, event: QtGui.QResizeEvent):
super().resizeEvent(event)
width = self.width()
# sizes = [self.sectionSize(self.logicalIndex(i)) for i in range(self.count())]
width_without_fixed = width - sum([self.sectionSize(i) for i in self.fixed_section_indexes])
for i in range(self.count()):
if not self.proportions:
break
if i not in self.fixed_section_indexes:
self.resizeSection(i, int(self.proportions[i] * width_without_fixed))
self._timer.start(1)
#pyqtSlot()
def _update_sizes(self):
width = self.width()
sizes = [self.sectionSize(self.logicalIndex(i)) for i in range(self.count())]
# width_without_fixed = width - sum([self.sectionSize(i) for i in self.fixed_section_indexes])
index = len(sizes) - 1
i = 0
while index >= 0 and sum(sizes) > width:
i += 1
if i > 100:
break
if sizes[index] > 5 and index not in self.fixed_section_indexes: # minimum width (5)
new_width = width - (sum(sizes) - sizes[index])
if new_width < 5:
new_width = 5
sizes[index] = new_width
index -= 1
for j, value in enumerate(sizes):
self.resizeSection(self.logicalIndex(j), value)
if not self.proportions:
self.proportions = [self.sectionSize(i) / width for i in range(self.count())]
class Model(QtCore.QAbstractTableModel):
def __init__(self, parent: Optional[QtWidgets.QWidget] = None) -> None:
super(Model, self).__init__(parent)
self.__headers = ["Column A", "Column B", "Column C", "Column D", "Column E", "Column F", "Column G"]
self.__data = []
for i in range(10):
row = [0, 1, 2, 3, 42222222222, 5, 6, 74444444]
self.__data.append(row)
def rowCount(self, index: Optional[QtCore.QModelIndex] = None) -> int:
return len(self.__data)
def columnCount(self, index: Optional[QtCore.QModelIndex] = None) -> int:
return len(self.__headers)
def headerData(self, section: int, orientation: QtCore.Qt.Orientation,
role: QtCore.Qt.ItemDataRole = Qt.ItemDataRole.DisplayRole) -> Any:
if role == Qt.ItemDataRole.DisplayRole:
if orientation == Qt.Orientation.Horizontal:
return self.__headers[section]
return f"{section}"
return None
def data(self, index: QtCore.QModelIndex,
role: QtCore.Qt.ItemDataRole = Qt.ItemDataRole.DisplayRole) -> Any:
if role in [Qt.ItemDataRole.DisplayRole, Qt.ItemDataRole.EditRole]:
return self.__data[index.row()][index.column()]
return None
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
view = QtWidgets.QTableView()
view.resize(600, 600)
header = HeaderView()
view.setHorizontalHeader(header)
model = Model()
view.setModel(model)
header.init_sizes()
view.horizontalHeader().resizeSection(0, 30)
view.horizontalHeader().setSectionResizeMode(0, QHeaderView.ResizeMode.Fixed)
view.horizontalHeader().resizeSection(1, 30)
view.horizontalHeader().setSectionResizeMode(1, QHeaderView.ResizeMode.Fixed)
view.show()
app.exec()

do all class functions need to be called to be used

I'm new to this so forgive me.
In the follow code on_draw(), on_update(), on_key_press(), and on_key_release() are never called but I know the code works so they do something, but why does it work?
I was under the impression that you had to do something like Mygame.on_update() to use a class function.
class Player(arcade.Sprite):
""" Player class """
def __init__(self, image, scale):
""" Set up the player """
# Call the parent init
super().__init__(image, scale)
# Create a variable to hold our speed. 'angle' is created by the parent
self.speed = 0
def update(self):
# Convert angle in degrees to radians.
angle_rad = math.radians(self.angle)
# Rotate the ship
self.angle += self.change_angle
# Use math to find our change based on our speed and angle
self.center_x += -self.speed * math.sin(angle_rad)
self.center_y += self.speed * math.cos(angle_rad)
class MyGame(arcade.Window):
"""
Main application class.
"""
def __init__(self, width, height, title):
"""
Initializer
"""
# Call the parent class initializer
super().__init__(width, height, title)
# Set the working directory (where we expect to find files) to the same
# directory this .py file is in. You can leave this out of your own
# code, but it is needed to easily run the examples using "python -m"
# as mentioned at the top of this program.
file_path = os.path.dirname(os.path.abspath(__file__))
os.chdir(file_path)
# Variables that will hold sprite lists
self.player_list = None
# Set up the player info
self.player_sprite = None
# Set the background color
arcade.set_background_color(arcade.color.BLACK)
def setup(self):
""" Set up the game and initialize the variables. """
# Sprite lists
self.player_list = arcade.SpriteList()
# Set up the player
self.player_sprite = Player(":resources:images/space_shooter/playerShip1_orange.png",
SPRITE_SCALING)
self.player_sprite.center_x = SCREEN_WIDTH / 2
self.player_sprite.center_y = SCREEN_HEIGHT / 2
self.player_list.append(self.player_sprite)
def on_draw(self):
"""
Render the screen.
"""
# This command has to happen before we start drawing
self.clear()
# Draw all the sprites.
self.player_list.draw()
def on_update(self, delta_time):
""" Movement and game logic """
# Call update on all sprites (The sprites don't do much in this
# example though.)
self.player_list.update()
def on_key_press(self, key, modifiers):
"""Called whenever a key is pressed. """
# Forward/back
if key == arcade.key.UP:
self.player_sprite.speed = MOVEMENT_SPEED
elif key == arcade.key.DOWN:
self.player_sprite.speed = -MOVEMENT_SPEED
# Rotate left/right
elif key == arcade.key.LEFT:
self.player_sprite.change_angle = ANGLE_SPEED
elif key == arcade.key.RIGHT:
self.player_sprite.change_angle = -ANGLE_SPEED
def on_key_release(self, key, modifiers):
"""Called when the user releases a key. """
if key == arcade.key.UP or key == arcade.key.DOWN:
self.player_sprite.speed = 0
elif key == arcade.key.LEFT or key == arcade.key.RIGHT:
self.player_sprite.change_angle = 0
def main():
""" Main function """
window = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
window.setup()
arcade.run()
if __name__ == "__main__":
main()

Memory game using pygame

I am making a memory game for an assignment. I can get all the tiles to be covered and make them reveal themselves upon being clicked on but I cannot cover them after they have been clicked on or match them. I dont know how to match the tiles also.
class Tile:
surface = None
border_size = 3
border_color = pygame.Color('black')
# An object in this class represents a Dot that moves
#classmethod
def set_surface(cls,game_surface):
cls.surface = game_surface
# instance method
def __init__(self,x , y, image, cover):
self.image = image
self.cover = cover
self.covered = True
self.time_cover = None
# self.timer = pygame.time.get_ticks()
width = self.image.get_width()
height = self.image.get_height()
self.rect = pygame.Rect(x, y, width, height)
def draw(self):
pygame.draw.rect(Tile.surface,Tile.border_color,self.rect,Tile.border_size)
Tile.surface.blit(self.image,self.rect)
if self.covered:
Tile.surface.blit(self.cover, self.rect)
def select(self, position):
valid_click = False
if self.rect.collidepoint(position):
if self.covered:
valid_click = True
self.expose_tile()
self.time_cover = pygame.time.get_ticks() + 2000
self.update()
else:
valid_click = False
return valid_click
def update(self):
if not self.covered and self.time_cover >= 2000:
self.covered = True
return self.covered
def expose_tile(self):
# if a tile is clicked this method will show the pic ture underneath that tile
self.covered = False
def __eq__ (self, other_tile):
pass
When you call update() in the main application loop, then revealed tiles will be covered after 2 seconds.
But you can add a cover_tile method, too:
class Tile:
# [...]
def cover_tile(self):
self.covered = True
If matching tiles share the same image (self.image), then matching tiles can be identified by comparing the images. e.g.:
(In the following tileA and tileB are instances of Tile)
if tielA.image != tileB.image:
tileA.cover_tile()
tileB.cover_tile()
else
print("matching")

Increase level when score reaches hundreds place

I have a game I am working on and am trying to increase the level every time I get to a hundred place, i.e., 100, 200, 300...etc.
Here is my function and it is probably something very simple I am forgetting to do
Thanks to #redunderthebed I discovered that the function was in the __init__() and that it was not getting into the if statement. So I moved my level changing into the if loop. So I moved it to my main() method and it still is not going into the if loop when the score goes to a 100 position. What am I doing wrong here?
# Pizza Panic
# Player must catch falling pizzas before they hit the ground
from livewires import games, color
from random import randrange
games.init(screen_width=640, screen_height=480, fps=50)
class Pan(games.Sprite):
""" A pan controlled by player to catch falling pizzas """
image = games.load_image("pan.bmp")
def __init__(self):
""" Initialize the pan object and create Text object for score """
super(Pan, self).__init__(image=Pan.image,
x=games.mouse.x,
bottom=games.screen.height)
self.score = games.Text(value=0, size=25, color=color.black,
top=5, right=games.screen.width - 10)
games.screen.add(self.score)
self.level = 0
def update(self):
""" move to mouse x position """
self.x = games.mouse.x
if self.left < 0:
self.left = 0
if self.right > games.screen.width:
self.right = games.screen.width
self.check_catch()
def check_catch(self):
""" Check if catch pizzas """
for pizza in self.overlapping_sprites:
self.score.value += 10
self.score.right = games.screen.width - 10
pizza.handle_caught()
class Pizza(games.Sprite):
""" a pizza which falls to the ground """
image = games.load_image("pizza.bmp")
speed = 1
def __init__(self, x, y=90):
""" Initialize a pizza object """
super(Pizza, self).__init__(image=Pizza.image,
x=x, y=y,
dy=Pizza.speed)
def update(self):
""" Check if bottom edge has reached screen bottom """
if self.bottom > games.screen.height:
self.end_game()
self.destroy()
def handle_caught(self):
""" Destroy self if caught """
self.destroy()
def end_game(self):
""" End the game """
end_message = games.Message(value="Game Over",
size=90,
color=color.red,
x=games.screen.width / 2,
y=games.screen.height / 2,
lifetime=5 * games.screen.fps,
after_death=games.screen.quit)
games.screen.add(end_message)
class Chef(games.Sprite):
""" A chef which moves left and right, dropping pizzas """
image = games.load_image("chef.bmp")
def __init__(self, y=55, speed=2, odds_change=200):
""" Initialize the chef object """
super(Chef, self).__init__(image=Chef.image,
x=games.screen.width / 2,
y=y,
dx=speed)
self.odds_change = odds_change
self.time_til_drop = 0
def update(self):
""" Determine if direction needs to be reversed """
if self.left < 0 or self.right > games.screen.width:
self.dx = -self.dx
elif randrange(self.odds_change) == 0:
self.dx = -self.dx
self.check_drop()
def check_drop(self):
""" Decrease countdown or drop pizza and reset countdown """
if self.time_til_drop > 0:
self.time_til_drop -= 1
else:
new_pizza = Pizza(x=self.x)
games.screen.add(new_pizza)
# set buffer to approx 30% of pizza height, regardless of pizza
# speed
self.time_til_drop = int(new_pizza.height * 1.3 / Pizza.speed) + 1
def main():
""" Play the game """
wall_image = games.load_image("wall.jpg", transparent=False)
games.screen.background = wall_image
the_chef = Chef()
games.screen.add(the_chef)
the_pan = Pan()
games.screen.add(the_pan)
score = the_pan.score.value
if score % 100 == 0:
the_pan.level += 1
the_pan.level_msg = games.Message(value="LEVEL {}"
.format(the_pan.level),
size=90,
color=color.red,
x=games.screen.width / 2,
y=games.screen.height / 2,
lifetime=5 * games.screen.fps,
after_death=None)
games.screen.add(the_pan.level_msg)
pizza = Pizza(x=0)
pizza.speed += 1
else:
print("not getting into the if statement")
games.mouse.is_visible = False
games.screen.event_grab = True
games.screen.mainloop()
# start it up
main()
I couldn't find any real documentation for Livewires, but I'm going to say based on your code (and assuming it's doing anything) you need to put your level check in here:
def update(self):
""" move to mouse x position """
self.x = games.mouse.x
if self.left < 0:
self.left = 0
if self.right > games.screen.width:
self.right = games.screen.width
self.check_catch()
self.check_level()
Perhaps check_level()
def check_level(self):
if not self.score.value % 100:
self.level += 1
self.level_msg = games.Message(value="LEVEL {}"
.format(the_pan.level),
size=90,
color=color.red,
x=games.screen.width / 2,
y=games.screen.height / 2,
lifetime=5 * games.screen.fps,
after_death=None)
games.screen.add(the_pan.level_msg)

How can I align a right-click context menu properly in PyQt?

With the sample code below (heavily influenced from here) the right-click context menu is not really aligned properly.
As can be seen in the screenshot, the resulting menu is above the mouse cursor quite a bit. I would expect the menu's top left corner to be exactly aligned with the mouse pointer.
Is there any way to adjust for this?
import re
import operator
import os
import sys
import sqlite3
import cookies
from PyQt4.QtCore import *
from PyQt4.QtGui import *
def main():
app = QApplication(sys.argv)
w = MyWindow()
w.show()
sys.exit(app.exec_())
class MyWindow(QWidget):
def __init__(self, *args):
QWidget.__init__(self, *args)
self.tabledata = [('apple', 'red', 'small'),
('apple', 'red', 'medium'),
('apple', 'green', 'small'),
('banana', 'yellow', 'large')]
self.header = ['fruit', 'color', 'size']
# create table
self.createTable()
# layout
layout = QVBoxLayout()
layout.addWidget(self.tv)
self.setLayout(layout)
def popup(self, pos):
for i in self.tv.selectionModel().selection().indexes():
print i.row(), i.column()
menu = QMenu()
quitAction = menu.addAction("Quit")
action = menu.exec_(self.mapToGlobal(pos))
if action == quitAction:
qApp.quit()
def createTable(self):
# create the view
self.tv = QTableView()
self.tv.setStyleSheet("gridline-color: rgb(191, 191, 191)")
self.tv.setContextMenuPolicy(Qt.CustomContextMenu)
self.tv.customContextMenuRequested.connect(self.popup)
# set the table model
tm = MyTableModel(self.tabledata, self.header, self)
self.tv.setModel(tm)
# set the minimum size
self.tv.setMinimumSize(400, 300)
# hide grid
self.tv.setShowGrid(True)
# set the font
font = QFont("Calibri (Body)", 12)
self.tv.setFont(font)
# hide vertical header
vh = self.tv.verticalHeader()
vh.setVisible(False)
# set horizontal header properties
hh = self.tv.horizontalHeader()
hh.setStretchLastSection(True)
# set column width to fit contents
self.tv.resizeColumnsToContents()
# set row height
nrows = len(self.tabledata)
for row in xrange(nrows):
self.tv.setRowHeight(row, 18)
# enable sorting
self.tv.setSortingEnabled(True)
return self.tv
class MyTableModel(QAbstractTableModel):
def __init__(self, datain, headerdata, parent=None, *args):
""" datain: a list of lists
headerdata: a list of strings
"""
QAbstractTableModel.__init__(self, parent, *args)
self.arraydata = datain
self.headerdata = headerdata
def rowCount(self, parent):
return len(self.arraydata)
def columnCount(self, parent):
return len(self.arraydata[0])
def data(self, index, role):
if not index.isValid():
return QVariant()
elif role != Qt.DisplayRole:
return QVariant()
return QVariant(self.arraydata[index.row()][index.column()])
def headerData(self, col, orientation, role):
if orientation == Qt.Horizontal and role == Qt.DisplayRole:
return QVariant(self.headerdata[col])
return QVariant()
def sort(self, Ncol, order):
"""Sort table by given column number.
"""
self.emit(SIGNAL("layoutAboutToBeChanged()"))
self.arraydata = sorted(self.arraydata, key=operator.itemgetter(Ncol))
if order == Qt.DescendingOrder:
self.arraydata.reverse()
self.emit(SIGNAL("layoutChanged()"))
if __name__ == "__main__":
main()
the position is in viewport coordinate, so if you are using
self.tableView.setContextMenuPolicy(Qt.CustomContextMenu)
so you don't have event passed to popup, you can do the following
action = menu.exec_(self.tableView.viewport().mapToGlobal(pos))
instead.
This was a bit tricky, but following the subclassing example in this wiki example and replacing
15 action = menu.exec_(self.mapToGlobal(event.pos()))
with
15 action = menu.exec_(event.globalPos())
will make the popup menu's top left corner match the mouse click exactly.
This will work for maximized/minified windows.
Menu will be generated at right-bottom position of mouse.
menu.exec_(self.mapToGlobal(self.mapFromGlobal(QtGui.QCursor.pos())))

Resources