python - kivy Spinner 打不开

标签 python python-3.x kivy kivy-language

我正在开发一个项目,但无法从 kivy 打开微调器小部件。 我在网上找不到任何答案,所以我来到这里:)

我尝试使用 float 布局而不是网格布局,但它也不起作用。 使用kivy==1.11.1(在2.0.0rc1和1.11.0中都不起作用)

UI.py

import kivy
kivy.require('1.11.1')

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import ObjectProperty
from kivy.uix.button import Button

from kivy.core.window import Window
Window.clearcolor = (1, 1, 1, 1)


from tkinter import Tk
from tkinter.filedialog import askopenfilename

class CustRedButton(Button):
    pass

class MainScreen(Screen):
    def OpenData(self):
        Tk().withdraw()  # we don't want a full GUI, so keep the root window from appearing
        dir = askopenfilename(title='Open File', filetypes=(('CSV files', '*.csv'),))
        if dir:
            self.DataFileDir=dir

    def toSettings(self):
        try:
            if self.DataFileDir:
                pass
            self.manager.current = 'settings'
        except Exception as e:
            if isinstance(e, AttributeError):
                print('Got No Data')
                self.manager.current = 'settings'
            else:
                print(e)


class SettingsScreen(Screen):
    def __init__(self, **kwargs):
        super(SettingsScreen, self).__init__(**kwargs)
        self.spinner_values = ('House', 'Home', 'Car', 'Kivy')

    def toMain(self):
        try:
            self.manager.current = 'main'
        except Exception as e:
            print(e)

    def on_spinner_select(self, text):
        print(text)


class ScreenManagment(ScreenManager):
    main_screen= ObjectProperty(None)
    settings_screen= ObjectProperty(None)


presentation= Builder.load_file('main.kv')
class MainApp(App):
    def build(self):
        return presentation

def runUI():
    MainApp().run()


if __name__ == '__main__':
    runUI()

和main.kv:

#: import NoTransition kivy.uix.screenmanager.NoTransition

<Button>:
    font_size: root.width*0.1
    size_hint: 0.2,0.17
    color: 0,0,0,1

    background_normal: ''
    background_color: 0.8,0.8,0.8,1

<CustRedButton>:
    font_size: root.width*0.19
    size_hint: 0.1,0.1
    color: 0,0,0,1

    background_normal: ''
    background_color: 1,0.1,0.1,1


<Label>:
    color: 0,0,0,1


ScreenManagment:
    id: screen_manager

    main_screen: main_screen
    settngs_screen: settings_screen

    transition: NoTransition()
    MainScreen:
        name: 'main'
        id: main_screen
        manager: screen_manager

    SettingsScreen:
        name: 'settings'
        id: settings_screen
        manager: screen_manager

<MainScreen>:
    BoxLayout:
        orientation:'horizontal'
        FloatLayout:
            Button:
                pos_hint: {'x':0.55,'top':0.6}
                on_press: root.toSettings()
                text: 'Go To Settings'

            Button:
                pos_hint: {'x':0.25,'top':0.6}
                text: 'Open Data'
                on_release: root.OpenData()


<SettingsScreen>:
    BoxLayout:
        orientation:'horizontal'
        FloatLayout:
            CustRedButton:
                pos_hint:{'x':0, 'y':0}
                on_press: root.toMain()
                text: 'To Main'

            GridLayout:
                cols:2
                pos_hint: {'x':-0.1, 'y':0.6}
                size_hint: 1, 0.2
                Label:
                    text:"Select"

                Spinner:
                    pos_hint: {'center': (.7, .7)}
                    text: root.spinner_values[0]
                    values: root.spinner_values
                    on_text: root.on_spinner_select(text)
                    background_color: 0.5,0.5,0.5,1

所有其他小部件都工作正常,并且微调器从 __ init __ Fine 获取“spinner_values” 实际上我不知道为什么我可以看到微调器但无法打开它来选择值。

附:我是 StackOverflow 的新手,所以如果我没有提供足够的信息,请告诉我,我会添加

最佳答案

问题是你的<Button>:规则应用于所有 Buttons ,包括 ButtonsSpinner 。将该规则更改为 MyButton@Button>并将其用于两个 Buttons ,让Spinner工作。这是您的 kv 的修改版本我认为该文件可以满足您的要求:

#: import NoTransition kivy.uix.screenmanager.NoTransition

<MyButton@Button>:
    font_size: root.width*0.1
    size_hint: 0.2,0.17
    color: 0,0,0,1

    background_normal: ''
    background_color: 0.8,0.8,0.8,1

<CustRedButton>:
    font_size: root.width*0.19
    size_hint: 0.1,0.1
    color: 0,0,0,1

    background_normal: ''
    background_color: 1,0.1,0.1,1


<Label>:
    color: 0,0,0,1


ScreenManagment:
    id: screen_manager

    main_screen: main_screen
    settngs_screen: settings_screen

    transition: NoTransition()
    MainScreen:
        name: 'main'
        id: main_screen
        manager: screen_manager

    SettingsScreen:
        name: 'settings'
        id: settings_screen
        manager: screen_manager

<MainScreen>:
    BoxLayout:
        orientation:'horizontal'
        FloatLayout:
            MyButton:
                pos_hint: {'x':0.55,'top':0.6}
                on_press: root.toSettings()
                text: 'Go To Settings'

            MyButton:
                pos_hint: {'x':0.25,'top':0.6}
                text: 'Open Data'
                on_release: root.OpenData()


<SettingsScreen>:
    BoxLayout:
        orientation:'horizontal'
        FloatLayout:
            CustRedButton:
                pos_hint:{'x':0, 'y':0}
                on_press: root.toMain()
                text: 'To Main'

            GridLayout:
                cols:2
                pos_hint: {'x':-0.1, 'y':0.6}
                size_hint: 1, 0.2
                Label:
                    text:"Select"

                Spinner:
                    pos_hint: {'center': (.7, .7)}
                    text: root.spinner_values[0]
                    values: root.spinner_values
                    on_text: root.on_spinner_select(text)
                    background_color: 0.5,0.5,0.5,1

关于python - kivy Spinner 打不开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60865253/

相关文章:

python - 将用户输入设置为变量名

python - 使用 sort() 还是 search()?

python - 调试器差异 : VSCode/Terminal (Python)

python - 简单代码中的列表索引超出范围错误

python - 注册用户名和密码

Python解析为小写并删除标点符号无法正常工作

python - Python 3.5 中的 Avro 编写器

python - 将 kivy 应用程序打包成一个 exe

python - KivMob 未加载广告

python - Kivy-ios Xcode构建错误