首页 > 解决方案 > Kivy 弹出窗口显示与主屏幕相同的按钮

问题描述

我对 Kivy 很陌生(已经使用了大约四个小时......)而且我遇到了弹出窗口。

我有一个主屏幕,在浮动布局中有四个按钮。按下时,我希望“移动”按钮打开一个弹出窗口。现在我已经完成了这项工作,但弹出窗口包含与我的主屏幕相同的四个按钮。

这是我的 Python 代码:

def show_movepop():
    show = MovePop()
    movepopWindow = Popup(title="Move", content=show, size_hint=(None, None),size=(400,400))
    movepopWindow.open()
    
class MovePop(FloatLayout):
    pass

class MainWindow(Screen):
    def movebtn(self):
        show_movepop()

class StatsWindow(Screen):
    pass

class WindowManager(ScreenManager):
    pass

kv = Builder.load_file("gamegui.kv")
           
class MainFloatApp(App):
    def build(self):
        return kv
        
if __name__ == "__main__":
    MainFloatApp().run()

这是我的 .kv 文件:

WindowManager:
    MainWindow:
    StatsWindow:

<Button>
    font_size:40
    color:0.3,0.6,0.7,1
    size_hint: 0.5, 0.1

<MainWindow>:
    name: "mainscreen"

    FloatLayout
        Button:
            text: "MOVE"
            id: move
            pos_hint: {"x":0, "y":0.1}
            on_release: root.movebtn()
            
        Button:
            text: "ACTION"
            id: action
            pos_hint: {"x":0.5, "y":0.1}
        
        Button:
            text: "EXAMINE"
            id: examine
            pos_hint: {"x":0, "y":0}
        
        Button:
            text: "STATS"
            id: stats
            pos_hint: {"x":0.5, "y":0}
            on_release: 
                app.root.current = "statsscreen"
                root.manager.transition.direction = "left"

<StatsWindow>:
    name: "statsscreen"
    Button:
        text: "Back"
        on_release:
            app.root.current = "mainscreen"
            root.manager.transition.direction = "right"

<MovePop>:
    Button: 
        text: "!"
        pos_hint: {"x":0.1, "y":0.5}
        on_release:

如果以上内容非常肮脏,请提前道歉,我效率不高:')

所有建议表示赞赏!

标签: pythonkivykivy-language

解决方案


好的,所以我不知道为什么,但这FloatLayout是导致问题的原因。

改变了

class MovePop(FloatLayout):
    pass

到:

class MovePop(AnchorLayout):
    pass

BoxLayout也摆脱了重复的按钮,但我无法按照我在该布局中想要的方式排列弹出窗口上的内容。


推荐阅读