繁体   English   中英

Kivy - 为什么按钮 state 的这种无关(看似)更改会影响清除列表的能力? Python

[英]Kivy - Why does this unrelated (seemingly) change of button state affect the ability to clear a list? Python

有人明白这里发生了什么吗? 目标:有很多按钮。 点击两个,他们都单独 append 一个项目到一个空列表。 当列表的 len 为 2 时(单击 2 个不同的按钮后),如果两个项目不相同,则清除/清空列表。 如果这两项相同,则清除/清空列表,并禁用这两个按钮。 memory 游戏就是这里的想法。 按钮从空白开始,单击它,它们显示文本,该文本是附加到列表中的项目。

问题:如果附加的两个项目匹配,它会按预期禁用按钮,但它不会再清空/清除列表。 为什么? 使用屏幕也很重要,因为它将成为不同应用程序中许多人之间的屏幕。

相关部分

                the_btn[0].text = ''
                the_btn[1].text = ''
                pairs.clear()
                the_btn.clear()
            elif pairs[0] == pairs[1]:
                the_btn[0].disabled = True
                the_btn[1].disabled = True
                pairs.clear()
                the_btn.clear()

完整代码:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen, ScreenManager


Builder.load_string("""

<ScreenOne>:
    StackLayout:
        Button:
            id: btn_1
            text:''
            size_hint: 0.2, 0.15
            on_release: 
                root.b1()
                root.check()
        Button:
            id: btn_2
            text:''
            size_hint: 0.2, 0.15
            on_release: 
                root.b2()
                root.check()
        Button:
            id: btn_3
            text:''
            size_hint: 0.2, 0.15
            on_release: 
                root.b3()
                root.check()
        Button:
            id: btn_4
            text:''
            size_hint: 0.2, 0.15
            on_release: 
                root.b4()
                root.check()
        Button:
            id: exit
            text:'exit'
            size_hint: 1, 0.15 
            on_release: app.stop()


""")

class ScreenOne(Screen):


    def b4(self):
        b4 = self.ids['btn_4']     
        b4.text = 'K'
    def b3(self):
        b3 = self.ids['btn_3']     
        b3.text = 'K'
    def b2(self):
        b2 = self.ids['btn_2']     
        b2.text = 'L'
    def b1(self):
        b1 = self.ids['btn_1']     
        b1.text = 'L'

    def check(self):
        buttons = [(self.ids['btn_1']), (self.ids['btn_2']), (self.ids['btn_3']), (self.ids['btn_4'])]
        pairs = []
        the_btn = []
  
               
        for x in buttons:
            pairs.append(x.text)
            if x.text != '':
                the_btn.append(x)
        for y in range(pairs.count('')):
            pairs.remove('')
        if len(pairs) == 2:           
            if pairs[0] != pairs[1]:
                the_btn[0].text = ''
                the_btn[1].text = ''
                pairs.clear()
                the_btn.clear()
            elif pairs[0] == pairs[1]:
                the_btn[0].disabled = True
                the_btn[1].disabled = True
                pairs.clear()
                the_btn.clear()

        print(the_btn)
        print(pairs)
    

screen_manager = ScreenManager()
screen_manager.add_widget(ScreenOne(name='one'))


class testApp(App):
    def build(self):
        return screen_manager



if __name__=='__main__':
    testApp().run()

为什么按钮被禁用后列表不会清除?
感谢您的意见

我认为问题不在于列表没有被清除(它正在被清除),而是每次执行check()都会将它们添加回列表中。 我认为,如果您将check()方法中列表的初始编译限制为非禁用Buttons ,它应该可以按您的意愿工作:

def check(self):
    buttons = [(self.ids['btn_1']), (self.ids['btn_2']), (self.ids['btn_3']), (self.ids['btn_4']),
               (self.ids['btn_5']), (self.ids['btn_6'])]
    pairs = []
    the_btn = []

    for x in buttons:
        if not x.disabled:  # ignore disabled buttons
            pairs.append(x.text)
            if x.text != '':
                the_btn.append(x)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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