在Kivy Python中弹出 - 在前面显示

时间:2016-09-23 18:14:06

标签: python popup kivy

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

这是我的简单代码,我想要做的是,而不是ModalView让Popup说“你好,你用X开始这个游戏”。问题是,当我使用Popup(也使用ModalView)时,窗口显示在每个按钮后面。当我在点击之后调用Popup时,Popup会正确显示,但我想在Window初始化时执行此操作。

1 个答案:

答案 0 :(得分:0)

我建议您在打开视图后打开弹出窗口,如

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