【问题标题】:<Kivy> Why is my python code not showing the buttons?<Kivy> 为什么我的 python 代码没有显示按钮?
【发布时间】:2019-03-30 18:18:56
【问题描述】:

以下代码的重点是显示一个按钮列表(如菜单),您可以使用这些按钮来更改屏幕,现在......它根本不起作用,我看不出在哪里问题是

我曾经得到“typeerror init() 需要 1 个位置参数,但给出了 2 个”,我通过为屏幕创建一个新类使其消失,然后我得到 app.root 实例不是设置,现在我没有收到任何错误

import kivy
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.lang import Builder
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.widget import Widget
import random
import main
kivy.require('1.10.1')

 class Game(BoxLayout):
    layout = BoxLayout(padding=5, orientation="vertical")
    colors = [(1, 0, 0, 1), (0, 1, 0, 1), (1, 0, 1, 1), (0, 0, 1, 1)]
    Buttonz= ('Button1', 'Button2', 'Button3', 'Button4')
    def Buttons(self):
        for i in range(len(self.Buttonz)):
            btn = Button(text=self.Buttonz[i],background_color=random.choice(self.colors))
            self.layout.add_widget(btn)
            btn.bind(on_press=main.Political)
        return self.layout


class Main(App):

    title='ButtonGame'
    def build(self):
       return Game()

if __name__ == '__main__':

   Main().run()

我希望代码显示 4 个按钮,这些按钮从我声明的 4 个中随机选择颜色,但它显示的是一个完全黑屏。这些按钮应该从不同的模块更改为另一个屏幕。也许我应该在两个模块中都使用 ScreenManager ??? (抱歉,我尝试将代码格式化为此处应有的格式,但我做不到)

【问题讨论】:

  • 缩进还是不对。请修复。 class Game(BoxLayout) 下面应该有缩进的东西,但没有。
  • 我刚刚修好了,希望没有遗漏帖子中的任何内容...

标签: python button widget kivy


【解决方案1】:

你需要添加一个构造函数来调用Buttons方法。

片段

class Game(BoxLayout):
    layout = BoxLayout(padding=5, orientation="vertical")
    colors = [(1, 0, 0, 1), (0, 1, 0, 1), (1, 0, 1, 1), (0, 0, 1, 1)]
    Buttonz = ('Button1', 'Button2', 'Button3', 'Button4')

    def __init__(self, **kwargs):
        super(Game, self).__init__(**kwargs)
        self.Buttons()
        self.add_widget(self.layout)

    def Buttons(self):
        for i in range(len(self.Buttonz)):
            btn = Button(text=self.Buttonz[i], background_color=random.choice(self.colors))
            self.layout.add_widget(btn)
            btn.bind(on_press=main.Political)

示例

以下示例仅使用一个BoxLayout 小部件作为根。

main.py

​​>
import kivy
kivy.require('1.10.1')

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
import random
# import main


class Game(BoxLayout):
    colors = [(1, 0, 0, 1), (0, 1, 0, 1), (1, 0, 1, 1), (0, 0, 1, 1)]
    Buttonz = ('Button1', 'Button2', 'Button3', 'Button4')

    def __init__(self, **kwargs):
        super(Game, self).__init__(**kwargs)
        self.padding = 5
        self.orientation = "vertical"
        self.Buttons()

    def Buttons(self):
        for i in range(len(self.Buttonz)):
            btn = Button(text=self.Buttonz[i], background_color=random.choice(self.colors))
            self.add_widget(btn)
            # btn.bind(on_press=main.Political)


class Main(App):
    title = 'ButtonGame'

    def build(self):
        return Game()


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

输出

【讨论】:

    猜你喜欢
    • 2020-10-13
    • 2021-01-03
    • 1970-01-01
    • 1970-01-01
    • 2020-12-11
    • 2022-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多