无法使用kivy languag切换屏幕

2024-10-05 10:20:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我对基维语还不熟悉。我正在尝试建立一个简单的程序来在两个屏幕之间切换。第一个屏幕包含一个按钮,松开按钮将切换到第二个屏幕。点击第二个屏幕上的按钮将进入第一个屏幕。你知道吗

我面临的问题: 1按钮放在角落里,我希望它的大小是全窗口,但它很小

  1. 单击并释放按钮不会显示任何效果。你知道吗

你知道吗千伏你知道吗

<ChatGUI>:
    MainManager:
        MainWindow:
        SecondWindow:

<MainWindow>:
    name: "main"
    Button:
        text:"to second window"
        on_release:app.root.current="second"

<SecondWindow>:
    name: "second"
    Button:
        text:"back to main"
        on_release:app.root.current="main"

python代码:

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.widget import Widget
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.scrollview import ScrollView
from kivy.graphics import Rectangle, Color, Canvas
from kivy.uix.screenmanager import ScreenManager,Screen
from kivy.core.window import Window
from kivy.config import Config
from kivy.lang import Builder

class ChatGUI(Widget):
    present=Builder.load_file("Chat.kv")
class MainWindow(Screen):
    pass
class SecondWindow(Screen):
    pass
class MainManager(ScreenManager):
    pass

class ChatApp(App):
    def build(self):
        return ChatGUI()

if __name__=="__main__":
    ChatApp().run()

我的输出#我无法添加图像,所以发布了输出链接

enter image description here

我在youtube教程上练习。你知道吗

我已经检查了许多来自堆栈溢出的代码,但在我的代码中没有发现问题。你知道吗

输出应显示占整个窗口大小的按钮,释放时应切换到下一个屏幕。你知道吗

你能告诉我有什么问题吗。你知道吗


Tags: namefromimportapp屏幕mainbutton按钮
2条回答

问题1-小部件@左下角&窗口未满?你知道吗

Button is placed on the corner and i am expecting its size to be full window but it small

根本原因

  1. 按钮小部件出现在左下角,因为根是Widget,没有提供位置(pos,或pos_hint)。因此,使用了默认位置(0,0)。你知道吗
  2. size不是完整窗口,因为默认情况下,Widget的大小是(100x100),或者默认的size_hint是(1,1)。你知道吗

Kivy Widget » Default values

  • A Widget is not a Layout: it will not change the position or the size of its children. If you want control over positioning or sizing, use a Layout.
  • The default size of a widget is (100, 100). This is only changed if the parent is a Layout. For example, if you add a Label inside a Button, the label will not inherit the button’s size or position because the button is not a Layout: it’s just another Widget.
  • The default size_hint is (1, 1). If the parent is a Layout, then the widget size will be the parent layout’s size.

问题2-释放按钮屏幕未切换?你知道吗

On click and release the button doesnt' show any effect.

根本原因

当按下按钮时,屏幕没有切换,因为应用程序的根不是ScreenManager。你知道吗

解决方案

解决这些问题有两种选择。你知道吗

选项1-使用布局作为根目录

此选项使用BoxLayout作为根,并需要以下增强。A Layout可以是GridLayoutBoxLayoutFloatLayout

Py文件

  1. 用BoxLayout替换小部件
  2. Builder.load_file(...)替换present = Builder.load_file(...)
  3. Builder.load_file(...)移出class ChatGUI()并添加pass

kv文件

  1. 在实例化对象下添加id: smMainManager:
  2. app.root.ids.sm.current替换app.root.current

片段-选项1

main1.py

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


Builder.load_file("main1.kv")


class ChatGUI(BoxLayout):
    pass


class MainWindow(Screen):
    pass


class SecondWindow(Screen):
    pass


class MainManager(ScreenManager):
    pass


class ChatApp(App):
    def build(self):
        return ChatGUI()


if __name__ == "__main__":
    ChatApp().run()

main1.kv

<ChatGUI>:
    MainManager:
        id: sm
        MainWindow:
        SecondWindow:

<MainWindow>:
    name: "main"
    Button:
        text: "to second window"
        on_release: app.root.ids.sm.current="second"

<SecondWindow>:
    name: "second"
    Button:
        text: "back to main"
        on_release: app.root.ids.sm.current="main"

选项2-使用ScreenManager作为根目录

此选项需要以下增强功能:

Py文件

  1. 删除导入语句,from kivy.uix.widget import Widget
  2. 删除class ChatGUI()
  3. return MainManager()替换return ChatGUI()
  4. Builder.load_file(...)替换present = Builder.load_file(...)

kv文件

  1. 删除kv文件中的类规则
  2. 用类规则替换MainManager::

片段-选项2

main2.py

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


Builder.load_file("main2.kv")


class MainWindow(Screen):
    pass


class SecondWindow(Screen):
    pass


class MainManager(ScreenManager):
    pass


class ChatApp(App):
    def build(self):
        return MainManager()


if __name__ == "__main__":
    ChatApp().run()

main2.kv

<MainManager>:
    MainWindow:
    SecondWindow:

<MainWindow>:
    name: "main"
    Button:
        text: "to second window"
        on_release: app.root.current="second"

<SecondWindow>:
    name: "second"
    Button:
        text: "back to main"
        on_release: app.root.current="main"

您不需要在Widget内添加ScreenManager。 所以呢

class ChatGUI (ScreenManager):

在python文件中

<ChatGUI>:
     MainWindow:
     SecondWindow:

在kv文件中 为了让你的榜样发挥作用,我只改变了这些。你知道吗

你知道吗千伏你知道吗

<ChatGUI>:
    MainWindow:
    SecondWindow:

<MainWindow>:
    name: "main"
    Button:
        text:"to second window"
        on_release:app.root.current="second"

<SecondWindow>:
    name: "second"
    Button:
        text:"back to main"
        on_release:app.root.current="main"

你知道吗主.py你知道吗

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.widget import Widget
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.scrollview import ScrollView
from kivy.graphics import Rectangle, Color, Canvas
from kivy.uix.screenmanager import ScreenManager,Screen
from kivy.core.window import Window
from kivy.config import Config
from kivy.lang import Builder

class ChatGUI(ScreenManager):
    present=Builder.load_file("Chat.kv")

class MainWindow(Screen):
    pass
class SecondWindow(Screen):
    pass

class ChatApp(App):
    def build(self):
        return ChatGUI()

if __name__=="__main__":
    ChatApp().run()

相关问题 更多 >

    热门问题