【发布时间】:2020-08-08 21:27:23
【问题描述】:
我对 Kivy 完全陌生,并试图制作这个应用程序屏幕,它基本上应该是一个蓝屏,其中心有一个白色矩形,该矩形中有一些文本和一个按钮。
我从网上获得了一些代码并尝试创建它,并且能够将我的背景设为蓝色并有一个白色矩形,但我无法将其居中。 不仅如此,当我运行代码时,它只在窗口的四分之一处给了我一个蓝色背景,在右上角有一个白色的 SQUARE - 屏幕的其余部分是黑色的!当我增加尺寸时,黑屏消失了,蓝色的 bg 屏幕在那里,但是现在左下角有一个白色的矩形。
我的代码如下所示。请有人向我解释我做错了什么,以及如何在没有任何黑屏的情况下将白色矩形居中放在蓝色上。谢谢!!
# import kivy module
import kivy
# this restrict the kivy version i.e
# below this kivy version you cannot
# use the app or software
kivy.require("1.9.1")
# base Class of your App inherits from the App class.
# app:always refers to the instance of your application
from kivy.app import App
# A Widget is the base building block
# of GUI interfaces in Kivy.
# It provides a Canvas that
# can be used to draw on screen.
from kivy.uix.widget import Widget
# From graphics module we are importing
# Rectangle and Color as they are
# basic building of canvas.
from kivy.graphics import Rectangle, Color
from kivy.uix.floatlayout import FloatLayout
from kivy.core.window import Window
self.size = Window.size
# class in which we are creating the canvas
class DeliverTodayWidget(FloatLayout):
def __init__(self, **kwargs):
super(DeliverTodayWidget, self).__init__(**kwargs)
self.size = Window.size
# Arranging Canvas
with self.canvas:
Color(.234, .456, .678, .8) # set the colour
# Seting the size and position of canvas
self.rect = Rectangle(pos=(self.center_x, self.center_y), size =(self.width,
self.height))
# Update the canvas as the screen size change
self.bind(pos=self.update_rect,size=self.update_rect)
Color(1, 0, 0, 0, 0.5) # set the colour
self.rect2 = Rectangle(pos=(self.center), size=(500,500)
self.bind(pos=self.update_rect2, size=self.update_rect2)
# update function which makes the canvas adjustable.
def update_rect(self, *args):
self.rect.pos = self.pos
self.rect.size = self.size
def update_rect2(self, *args):
self.rect2.pos = self.center
class DeliverTodayApp(App):
def build(self):
return DeliverTodayWidget()
DeliverTodayApp().run()
我已经浏览了许多网站来搜索有关使矩形居中的内容,包括堆栈溢出的其他一些页面,但没有任何帮助。任何帮助将不胜感激。
(P.S. 我使用的是 macOS 和 pycharm)
【问题讨论】:
标签: python python-3.x macos syntax kivy