Created
July 20, 2020 04:33
-
-
Save s-kganz/f7376cc683185d3ec5f704bf133d00c3 to your computer and use it in GitHub Desktop.
Blocking Kivy application
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#: kivy 1.11.1 | |
<MainLayout@BoxLayout>: | |
palindrome_label: palindrome_label | |
prime_label: prime_label | |
prime: 100000000 | |
palindrome: 10000001 | |
orientation: 'vertical' | |
BoxLayout: | |
orientation: 'horizontal' | |
Label: | |
id: palindrome_label | |
text: 'The current palindrome is {}'.format(root.palindrome) | |
Label: | |
id: prime_label | |
text: 'The current prime is {}'.format(root.prime) | |
Splitter: | |
sizable_from: 'top' | |
BoxLayout: | |
orientation: 'horizontal' | |
Button: | |
text: 'Get next palindrome' | |
on_press: app.update_palindrome() | |
Button: | |
text: 'Get next prime' | |
on_press: app.update_prime() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from concurrent.futures import ThreadPoolExecutor | |
import kivy | |
from kivy.app import App | |
from kivy.properties import ObjectProperty, NumericProperty | |
from kivy.uix.boxlayout import BoxLayout | |
def is_prime(n): | |
''' | |
Checks if a number is prime. | |
''' | |
for i in range(2, n): | |
if n % i == 0: return False | |
return True | |
def get_next_prime(n): | |
''' | |
Returns the next prime number larger than n. | |
''' | |
n += 1 | |
while not is_prime(n): n += 1 | |
return n | |
def get_next_palindrome(n): | |
''' | |
Returns the next palindromic number larger than n. | |
''' | |
n += 1 | |
while not str(n) == str(n)[::-1]: n += 1 | |
return n | |
class MainLayout(BoxLayout): | |
palindrome_label = ObjectProperty(None) | |
prime_label = ObjectProperty(None) | |
palindrome = NumericProperty(1) | |
prime = NumericProperty(1) | |
class AsyncApp(App): | |
def build(self): | |
self.layout = MainLayout() | |
return self.layout | |
def update_palindrome(self): | |
current = self.layout.palindrome | |
self.layout.palindrome = get_next_palindrome(current) | |
def update_prime(self): | |
current = self.layout.prime | |
self.layout.prime = get_next_prime(current) | |
if __name__ == '__main__': | |
AsyncApp().run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment