【问题标题】:Kivy Button on_press function=Popup not calling functionKivy Button on_press function=Popup 不调用函数
【发布时间】:2020-08-08 08:41:28
【问题描述】:

这是有问题的代码:

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

我遇到了错误:

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

AssertionError: None is not callable

当为列表“li”中的每个元素按下任何按钮时,我想获得一个带有 title = "ttl" 和 text = "txt" 变量的弹出窗口

请放心,我是初学者

【问题讨论】:

    标签: python popup kivy-language popupwindow


    【解决方案1】:

    您试图在按下按钮时获取文本 为什么不直接从按钮获取文本

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多