简体   繁体   中英

Kivy Button on_press function=Popup not calling function

Here's the code that has the problem:

from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.uix.scrollview import ScrollView
from kivy.core.window import Window
from kivy.properties import StringProperty
from kivy.uix.label import Label
from kivy.app import runTouchApp
from kivy.uix.popup import Popup
import arabic_reshaper
import bidi.algorithm

file = open("/sdcard/ls.txt","r")
li = [i for i in open("/sdcard/code_pinal.txt").read().split('\n\n\n')]

class MessageBox(Popup):
    message = StringProperty()
def message_box(self, message):
    p = MessageBox()
    p.message = message
    p.open() 

layout = GridLayout(cols=1, spacing=1, size_hint_y=None)
# Make sure the height is such that there is something to scroll.
layout.bind(minimum_height=layout.setter('height'))

for x in li:
    ttl = bidi.algorithm.get_display(arabic_reshaper.reshape(x.split(':')[0]))
    txt = bidi.algorithm.get_display(arabic_reshaper.reshape(x.split(':')[1]))
    btn = Button(text=str(title), font_name='/storage/emulated/0/Download/fonts/DejaVuSans.ttf', size_hint_y=None, height=90, on_press=message_box(text))
    layout.add_widget(btn)

root = ScrollView(size_hint=(1, None), size=(Window.width, Window.height))
root.add_widget(layout)

runTouchApp(root)

https://i.stack.imgur.com/u2ePj.jpg

i'm getting errors:

TypeError: message_box() missing 1 required positional argument: 'message'

and

AssertionError: None is not callable

i want to get a popup with the title = "ttl" and text = "txt" varibles when any button pressed for every element on the list "li"

please take it easy with me, i'm Beginner

your trying to get text of button on press why don't you just get the text from button

from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.uix.scrollview import ScrollView
from kivy.core.window import Window
from kivy.properties import StringProperty
from kivy.uix.label import Label
from kivy.app import runTouchApp
from kivy.uix.popup import Popup
import arabic_reshaper
import bidi.algorithm

file = open("/sdcard/ls.txt","r")
li = [i for i in open("/sdcard/code_pinal.txt").read().split('\n\n\n')]

class MessageBox(Popup):
    message = StringProperty()
def message_box(self, btn):
    message = btn.text
    p = MessageBox()
    p.message = message
    p.open() 

layout = GridLayout(cols=1, spacing=1, size_hint_y=None)
# Make sure the height is such that there is something to scroll.
layout.bind(minimum_height=layout.setter('height'))

for x in li:
    ttl = bidi.algorithm.get_display(arabic_reshaper.reshape(x.split(':')[0]))
    txt = bidi.algorithm.get_display(arabic_reshaper.reshape(x.split(':')[1]))
    btn = Button(text=str(title), font_name='/storage/emulated/0/Download/fonts/DejaVuSans.ttf', size_hint_y=None, height=90, on_press=message_box)
    layout.add_widget(btn)

root = ScrollView(size_hint=(1, None), size=(Window.width, Window.height))
root.add_widget(layout)

runTouchApp(root)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM