简体   繁体   中英

Popup in Kivy Python - show in front

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.popup import Popup
from kivy.uix.label import Label
from kivy.uix.stacklayout import StackLayout
from kivy.uix.modalview import ModalView
from kivy.config import Config
from ai import Ai
from random import randint

class TicTacToe(StackLayout): #StackLayout explanation: https://kivy.org/docs/api-kivy.uix.stacklayout.html

    states = [1, 2, 3, 4, 5, 6, 7, 8, 9]
    choices = ["X","O"]

    def __init__(self, **kwargs):
        super(TicTacToe, self).__init__(**kwargs)
        self.init_players();

        for x in range(9): # range() explanation: http://pythoncentral.io/pythons-range-function-explained/
            bt = Button(text=' ', font_size=200, width=200, height=200, size_hint=(None, None), id=str(x+1))
            bt.bind(on_release=self.btn_pressed)
            self.add_widget(bt)

        view = ModalView(auto_dismiss=False)
        view.add_widget(Label(text='Hello world'))
        view.open()

    def init_players(self):
        rand_choice = randint(0,1);
        self.bot = Ai(self.choices[rand_choice]);
        self.player = self.choices[0] if rand_choice == 1 else self.choices[1]

    def btn_pressed(self, button):
        button.text="X"
        self.states[int(button.id)-1] = "X"
        print(self.states)

class MyApp(App):

    title = 'Tic Tac Toe'

    def build(self):
        Config.set('graphics', 'width', '600')
        Config.set('graphics', 'height', '600')
        return TicTacToe()



if __name__ == '__main__':
    MyApp().run()

This is my simple code, what I am trying to do is, instead of ModalView to have a Popup saying "Hello, you started this game with X". The problem is that when I use Popup (with ModalView too), the window is being displayed behind every button. When I call the Popup after a click though, the Popup is shown properly, but I want to do this on Window initialization.

I suggest you simply open the pop-up after opening your view like

view = ModalView(auto_dismiss=False)
view.add_widget(Label(text='Hello world'))
view.open()

popup = Popup(
    title=title,
    content=content,
    auto_dismiss=True)

popup.open()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM