Reputation: 1
I'm making a game and trying to place a button at the center of the screen. However, using center_x and center_y does not correctly center the button. I attempted to use a GridLayout with 3 columns and placed the button in the 5th cell (out of 9), but the grid itself does not seem centered.
I suspect that self.width and self.height might be the issue, but another part of my code (which draws a background grid) uses these values without any problems.
Summary of my issue: Expected behavior: The grid and button should be centered on the screen. Actual behavior: The grid itself is not centered. Assumptions: self.width and self.height should be equal to the window size, and the window size does not change.
Code:
from kivy.config import Config
Config.set('input', 'wm_pen', 'mouse')
Config.set('graphics', 'dpi', '96')
Config.set('input', 'wm_touch', 'none')
Config.set('graphics', 'fullscreen', 'auto') # Start in fullscreen mode
Config.set('graphics', 'borderless', '1')
Config.set('graphics', 'resizable', '0')
Config.write()
from kivy.core.window import Window
import sys
from kivy.metrics import dp
from kivy.uix.effectwidget import Rectangle
from kivy.uix.screenmanager import FallOutTransition
from kivy.uix.filechooser import string_types
from kivy.uix.accordion import ObjectProperty
import random
from kivy import platform
from kivy.core.window import Window
from kivy.app import App
from kivy.graphics.context_instructions import Color
from kivy.graphics.vertex_instructions import Line, Quad, Triangle
from kivy.properties import NumericProperty, Clock, ObjectProperty
from kivy.uix.widget import Widget
from kivy.uix.relativelayout import RelativeLayout
from kivy.lang.builder import Builder
from kivy.uix.image import Image
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.label import Label
from kivy.metrics import Metrics
from kivymd.uix.button import MDRectangleFlatButton
from kivymd.theming import ThemeManager
from kivymd.app import MDApp
from kivy.uix.gridlayout import GridLayout
from kivy.uix.accordion import BooleanProperty
class Background(Widget):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.width,self.height= Window.size
def grid_drawing (self):
self.canvas.clear()
with self.canvas:
# Draw the smaller squares
Color (1,1,1, 0.9)
Rectangle(pos=(0,0), size=(self.width, self.height))
Color(0.6, 0.6, 0.6, 0.5) # Light gray
for x in range(0, self.width, int(self.width/30)): # Adjust 20 for smaller squares
for y in range(0, self.height, int(self.height/30)):
Line(rectangle=(x, y, self.width, self.height), width=1)
Color (0.9,0.9,0.9,1)
def start_screen(self):
print (self.height, self.width)
self.size = Window.size
self.theme_cls = ThemeManager()
layout = GridLayout(cols=3, spacing=10, size_hint=(None, None))
#layout.size = (dp(500), dp(200)) # Set a fixed size
layout.pos_hint = {"center_x": 0.5, "center_y": 0.5} # Center the grid
start_button = MDRectangleFlatButton (
text="Start",
# pos_hint= {"center_x": 1, "center_y" : 1},
# pos = (dp(self.width/2), dp (self.height/2)),
# size_hint = (3,1)
)
for i in range (4):
layout.add_widget(Label(text= "", size_hint_x=None, width=100 ))
layout.add_widget(start_button)
for i in range (4):
layout.add_widget(Label(text= "", size_hint_x=None, width=100 ))
self.add_widget(layout)
class Gamewindow (FloatLayout):
stage= 0
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.lvl_change = BooleanProperty (False)
self.Graph_paper= Background(size_hint=(1,1), pos_hint={'x': 0, 'y': 0})
self.stage +=1
Clock.schedule_once(self.config_lvl, 1/50)
self.add_widget(self.Graph_paper)
def config_lvl (self, dt):
# for child in self.Graph_paper.children[:]:
# self.Graph_paper.remove_widget(child)
if not self.lvl_change: # Only update if there's a change
return
self.lvl_change = False
self.Graph_paper.grid_drawing ()
if self.stage== 1:
self.Graph_paper.start_screen()
class Steve(MDApp):
def build(self):
# Return the main window class as the root widget
return Gamewindow()
if __name__ == "__main__":
Steve().run()
Things I've Tried
Checked if self.width and self.height changed before start_screen() → They did not, the values remained correct. Tried using FloatLayout instead of GridLayout → The issue persisted.
Upvotes: -1
Views: 35