我可以用kivy中的屏幕替换小部件

发布于 2025-02-02 15:27:57 字数 2558 浏览 2 评论 0原文

我是Kivy的初学者,我有一个FCLASS(小部件),我希望它是FCLASS(屏幕),但是当我试图将所有屏幕弄乱时,代码会生成一些按钮,我希望我在浮点布局上也可以做同样的事情,但是我希望FCLASS保留一个屏幕,因为我正在构建一个多屏应用程序。

这是.py文件:

import kivy

from kivy.uix.gridlayout import GridLayout
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.widget import Widget 
from kivy.uix.textinput import TextInput
from kivy.clock import Clock
from kivy.uix.image import Image
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.core.window import Window
from kivy.config import Config
from kivy.lang import Builder
from kivy.properties import StringProperty, ObjectProperty, NumericProperty,ReferenceListProperty
from kivy.graphics.texture import Texture
from kivy.core.camera import Camera
from kivy.graphics import *
import time
import os 
from pathlib import Path 
#import cv2                                         
import struct
import threading
import pickle

Builder.load_file('the.kv')
  

class fscreen(Widget):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self.list_of_btns = []


    def create(self):
        self.h = self.height*0.9
        for i in range(4):
            self.h = self.h - self.height*0.1
            self.btn = Button(text='button '+str(i), size=(self.width*0.4,self.height*0.05), pos=(self.width*0.3, self.h), on_press=self.press)
            self.list_of_btns.append(self.btn)
            self.add_widget(self.btn)


    def press(self, instance):
        print(instance.text)


    def delete(self):
        for btns in self.list_of_btns:
            self.remove_widget(btns)


class theapp(App):
    def build(self):
    
        self.screenm = ScreenManager() 

        self.fscreen = fscreen()
        screen = Screen(name = "first screen")
        screen.add_widget(self.fscreen)
        self.screenm.add_widget(screen)


        return self.screenm

if __name__ == "__main__":
    theapp = theapp()                                       #
    theapp.run() 

.kv文件:

<fscreen>
    Button:
        text: 'create'
        size: root.width*0.4, root.height*0.05
        pos: root.width*0.3, root.height*0.1
        on_press: root.create()



    Button:
        text: 'delete'
        size: root.width*0.4, root.height*0.05
        pos: root.width*0.3, root.height*0.2
        on_press: root.delete()

如何在不弄乱所有内容的情况下使FCLASS成为屏幕类? 先感谢您

I'm a beginner at kivy, I have an fclass(Widget) that I want it to be a fclass(Screen), but when I tried to make the change all the screen messed up, the code generate some buttons with a for loop, I wish I coud do the same with a float layout, but I want the fclass to stay a screen since I'm building a multiscreen app.

Here is the .py file:

import kivy

from kivy.uix.gridlayout import GridLayout
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.widget import Widget 
from kivy.uix.textinput import TextInput
from kivy.clock import Clock
from kivy.uix.image import Image
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.core.window import Window
from kivy.config import Config
from kivy.lang import Builder
from kivy.properties import StringProperty, ObjectProperty, NumericProperty,ReferenceListProperty
from kivy.graphics.texture import Texture
from kivy.core.camera import Camera
from kivy.graphics import *
import time
import os 
from pathlib import Path 
#import cv2                                         
import struct
import threading
import pickle

Builder.load_file('the.kv')
  

class fscreen(Widget):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self.list_of_btns = []


    def create(self):
        self.h = self.height*0.9
        for i in range(4):
            self.h = self.h - self.height*0.1
            self.btn = Button(text='button '+str(i), size=(self.width*0.4,self.height*0.05), pos=(self.width*0.3, self.h), on_press=self.press)
            self.list_of_btns.append(self.btn)
            self.add_widget(self.btn)


    def press(self, instance):
        print(instance.text)


    def delete(self):
        for btns in self.list_of_btns:
            self.remove_widget(btns)


class theapp(App):
    def build(self):
    
        self.screenm = ScreenManager() 

        self.fscreen = fscreen()
        screen = Screen(name = "first screen")
        screen.add_widget(self.fscreen)
        self.screenm.add_widget(screen)


        return self.screenm

if __name__ == "__main__":
    theapp = theapp()                                       #
    theapp.run() 

The .kv file:

<fscreen>
    Button:
        text: 'create'
        size: root.width*0.4, root.height*0.05
        pos: root.width*0.3, root.height*0.1
        on_press: root.create()



    Button:
        text: 'delete'
        size: root.width*0.4, root.height*0.05
        pos: root.width*0.3, root.height*0.2
        on_press: root.delete()

How can I make the fclass a screen class without messing up everything ?
Thank you in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜声 2025-02-09 15:27:57

在我看来,代码应该起作用,但事实并非如此。一个修复程序是在您的size> size_hint中使用size在您的kvpy中。因此,kv可以看起来像:

<fscreen>:
    Button:
        text: 'create'
        size_hint: 0.4, 0.05
        # size: root.width*0.4, root.height*0.05
        pos: root.width*0.3, root.height*0.1
        on_press: root.create()



    Button:
        text: 'delete'
        size_hint: 0.4, 0.05
        # size: root.width*0.4, root.height*0.05
        pos: root.width*0.3, root.height*0.2
        on_press: root.delete()

create()方法:

def create(self):
    self.h = self.height * 0.9
    for i in range(4):
        self.h = self.h - self.height * 0.1
        self.btn = Button(text='button ' + str(i), size_hint=(0.4, 0.05),
                          pos=(self.width * 0.3, self.h), on_press=self.press)
        self.list_of_btns.append(self.btn)
        self.add_widget(self.btn)

您的size sizekv和<无论如何,代码> py 都只是尝试执行size_hint

而且,当然,必须调整您的build()方法:

def build(self):
    self.screenm = ScreenManager()

    self.fscreen = fscreen(name="first screen")
    self.screenm.add_widget(self.fscreen)

    return self.screenm

其他要注意的内容:

  • 您应该使用上层名称的上限。如果不这样做,可能会导致kv中的错误,
  • 您应该考虑使用pos_hint而不是pos,以更好地调整应用程序的大小

Seems to me that code should work, but it doesn't. A fix is to use size_hint instead of size in both your kv and py. So the kv could look like:

<fscreen>:
    Button:
        text: 'create'
        size_hint: 0.4, 0.05
        # size: root.width*0.4, root.height*0.05
        pos: root.width*0.3, root.height*0.1
        on_press: root.create()



    Button:
        text: 'delete'
        size_hint: 0.4, 0.05
        # size: root.width*0.4, root.height*0.05
        pos: root.width*0.3, root.height*0.2
        on_press: root.delete()

and in the create() method:

def create(self):
    self.h = self.height * 0.9
    for i in range(4):
        self.h = self.h - self.height * 0.1
        self.btn = Button(text='button ' + str(i), size_hint=(0.4, 0.05),
                          pos=(self.width * 0.3, self.h), on_press=self.press)
        self.list_of_btns.append(self.btn)
        self.add_widget(self.btn)

Your size in the kv and py were both just trying to do the size_hint anyway.

And, of course, your build() method must be adjusted:

def build(self):
    self.screenm = ScreenManager()

    self.fscreen = fscreen(name="first screen")
    self.screenm.add_widget(self.fscreen)

    return self.screenm

Other things to note:

  • You should use upper case for class names. Failure to do so can lead to errors in kv
  • You should consider using pos_hint instead of pos to allow better resizing of your App
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文