所以我正在为一个类项目做一个游戏,我想添加一个按钮网格,大小不重要,但经过2天的尝试,我迷失了这里是我的Python代码:
import numpy as np
from collections import namedtuple
from kivy.app import App
from kivy.clock import Clock
from kivy.core.audio import SoundLoader
from kivy.core.window import Window
from kivy.graphics import Canvas
from kivy.graphics import Rectangle
from kivy.lang import Builder
from kivy.properties import ObjectProperty
from kivy.properties import ListProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.togglebutton import ToggleButton
from kivy.uix.screenmanager import NoTransition
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.widget import Widget
field = namedtuple('field', ['rows', 'cols'])
FIELD = field(30, 60)
global a
a = "play"
class Jeu(Screen):
#here is the screen for the button grid
#
#
#
def test(self):
Clock.schedule_interval(the_callback,0.1)
def stop(self):
Clock.unschedule(the_callback)
class MenuScreen(Screen):
pass
class RuleScreen (Screen):
pass
class SettingsScreen(Screen):
sound=SoundLoader.load('oui.wav')
def fullscreen(self):
name = self.ids.fullscreen.text
if Window.fullscreen == True:
self.ids.fullscreen.text = 'Fullscreen off'
Window.fullscreen = False
else:
self.ids.fullscreen.text = 'Fullscreen on'
Window.fullscreen = True
def music(self):
sound=SoundLoader.load('oui.wav')
name = self.ids.music.text
global a
if a == "play":
self.ids.music.text = 'Music off'
a= "stop"
else:
self.ids.music.text = 'Music on'
a= "play"
class PloytestApp(App):
def build(self):
# Create the screen manager
sm = ScreenManager()
sm.add_widget(MenuScreen(name='menu'))
sm.add_widget(Jeu(name='jeu'))
sm.add_widget(RuleScreen(name='rules'))
sm.add_widget(SettingsScreen(name='settings'))
sm.transition = NoTransition()
return sm
sound=SoundLoader.load('oui.wav')
def my_callback(dt):
if a=="stop":
sound.stop()
elif a =="play":
sound.play()
event = Clock.schedule_interval(my_callback, 1 / 30.)
def the_callback(dt):
print("aaaaa")
if __name__ == '__main__':
PloytestApp().run()
App.stop(exit())
还有我的kv女孩
#: import Window kivy.core.window.Window
#: import Clock kivy.clock
<MenuScreen>:
GridLayout:
cols: 1
Image:
id: gif
source: 'test.zip'
allow_stretch: True
keep_aspect: False
anim_loop: 0
Label:
pos: self.parent.pos
size: self.parent.size
text:"Connway's game of life"
font_size: 60
color: 0.5, 0.8, 1, 1
GridLayout:
cols: 1
Button:
text: 'Play'
on_release:
root.manager.current = 'jeu'
Button:
text: 'How to Play'
on_release:
root.manager.current = 'rules'
Button:
text: 'Goto settings'
on_release:
root.manager.current = 'settings'
Button:
text: 'Quit'
on_release: app.stop()
<Jeu>:
GridLayout:
cols:1
Label:
** #here is the grid of button**
text: "here the grid of button"
GridLayout:
cols:4
ToggleButton:
id:start
text: 'Start simulation'
on_state:
if self.state == 'normal':\
print("aa")
root.test()
if self.state == 'normal':\
root.stop()
Button:
id:next
text: 'Next Generation'
Button:
id:clear
text: 'Clear'
on_press:
start.state ='normal' if start.state == 'down' else 'normal'
Button:
text:"Back to menu"
on_press:
start.state ='normal' if start.state == 'down' else 'normal'
on_release:
root.manager.current = 'menu'
<RuleScreen>:
GridLayout:
cols:1
Image:
source: 'rules.jpg'
GridLayout:
cols: 2
Button:
text: 'Setting'
on_release:
root.manager.current = 'settings'
Button:
text: 'Back to menu'
on_release:
root.manager.current = 'menu'
<SettingsScreen>:
GridLayout:
cols: 1
size: root.width - 100, root.height -100
Button:
id: fullscreen
text: "Fullscreen off"
on_release:
root.fullscreen()
Button:
id: music
text: "Music on"
on_release:
root.music()
Button:
text: 'Back to menu'
on_press:
root.manager.current = 'menu'
我试着放一个for循环,但是没有成功,我试着把“Jeu”的类从Screen改为GridLayout,它不允许启动程序。我只想要一个切换按钮的网格10x10是一个好的开始。
我很抱歉的英语水平,它不是我的主要语言,所以可能有我的代码中的法语。此外,我使用thonny代码,因为我懒,它的程序学校让我们安装。提前感谢的帮助。
1条答案
按热度按时间vu8f3i0k1#
您可能希望将
RecycleView
与RecycleGridLayout
一起使用,如this post所示。file.py
ploytest.kv
如果您需要为每个按钮应用自定义文本,那么可以使用如下方法
第一行声明了一个名为
texts
的列表,您可以在其中放置100个不同的文本。最后,如果你想给每个按钮添加额外的属性,你可以在kv文件的
data
字典中添加任意多的属性。例如,如果你想给按钮的文本添加一些红色:编辑