Kivy Python中的弹出窗口显示fron

2024-06-01 00:06:41 发布

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

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有一个弹出窗口说“你好,你用X开始了这个游戏”。问题是,当我使用Popup(与ModalView一起使用)时,窗口显示在每个按钮后面。当我在点击后调用弹出窗口时,弹出窗口显示正确,但我想在窗口初始化时执行此操作。在


Tags: fromimportselfconfiginitdefrangebutton
1条回答
网友
1楼 · 发布于 2024-06-01 00:06:41

我建议你在打开视图后打开弹出窗口

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

相关问题 更多 >