Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
400 views
in Technique[技术] by (71.8m points)

python - Kivy Popup Shows Same Buttons as Main Screen

I'm very new to Kivy (been using for about four hours...) and I've hit a wall with popups.

I have a main screen which has four buttons in a float layout. On press down I want the 'MOVE' button to open a popup. Now I've got this working but the popup contains the same four buttons as my mainscreen.

This is my Python code:

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()

and this is my .kv file:

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:

Apologies in advance if the above is super dirty, I'm not very efficient :')

All suggestions appreciated!

question from:https://stackoverflow.com/questions/66049616/kivy-popup-shows-same-buttons-as-main-screen

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Okay so I don't know why but it was the FloatLayout that was causing the problem.

Changed

class MovePop(FloatLayout):
    pass

to:

class MovePop(AnchorLayout):
    pass

BoxLayout also got rid of the duplicate buttons but I couldn't arrange the content on the popup the way I wanted in that layout.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...