gpt4 book ai didi

python - Kivy多屏管理

转载 作者:行者123 更新时间:2023-12-04 01:08:51 26 4
gpt4 key购买 nike

我对 Kivy 还很陌生(两天前开始学习)我正在研究一个基本的计算器,但遇到了一个我无法跳过的障碍。
我想创建多个屏幕,因为我打算在我的计算器中添加更多内容,因为我正在进一步学习 Kivy 并且我不知道如何在我的代码中调整 ScreenManager。

这是我的 .py 文件

import kivy

kivy.require('1.11.0')
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.pagelayout import PageLayout
from kivy.core.window import Window

Window.clearcolor = .3,.3,.3,1

class RootWidget(GridLayout):
def calculate(self, calculation):
if calculation:
try:
self.display.text = str(eval(calculation))
except Exception:
self.display.text = "Error"
class kutuApp(App):
def build(self):
return RootWidget()
if __name__== '__main__':
kutuApp().run()

这是我的 .kv 文件

<CustButton@Button>:
font_size: 35
background_color: 0,0,0,0
canvas.before:
Color:
rgba: (.4, .4, .4, 1) if self.state=='normal' else (0,.7,.7,1)
RoundedRectangle:
pos: self.pos
size: self.size
radius: [20, ]

<RawLayout@BoxLayout>:
spacing: 8
padding: 8
size_hint: [1, .2]

<RootWidget>:
id: calculator
rows: 10
display: entry
spacing: 1

BoxLayout:
size_hint: [1, .1]
Label:
text: 'Basic Calculator'
Label:
text: 'Made by Xrew'

BoxLayout:
padding: 10
TextInput:
id: entry
spacing: 1
padding: 5
font_size: 32
multiline: True
focus: False
# background_color: 0, 0, 0, 1
# foreground_color: [1, 0, 1, 1]

RawLayout:
CustButton:
text: '<'
on_press: entry.text += self.text
CustButton:
text: '>'
on_press: entry.text += self.text
CustButton:
text: '≈'
on_press: entry.text += '=='

RawLayout:
orientation: 'horizontal'
CustButton:
text: '('
on_press: entry.text += '('
CustButton:
text: ')'
on_press: entry.text += ')'
CustButton:
text: '√'
on_press: entry.text += '**(.5)'
CustButton:
text: '¹/x'
on_press: entry.text += '1/'
RawLayout:
orientation: 'horizontal'
CustButton:
text: 'Del'
on_press: entry.text = entry.text[:-1]
CustButton:
text: 'x²'
on_press: entry.text += '**2'
CustButton:
text: 'xⁿ'
on_press: entry.text += '**'
CustButton:
text: 'π'
on_press: entry.text += '3.14'

RawLayout:
orientation: 'horizontal'
cols: 4
CustButton:
text: 'Clr'
font_color: [255,0,0,1]
# background_normal: ' '
# background_color: 1, .3, .4, .85
on_press: entry.text = ""
CustButton:
text: '+'
on_press: entry.text += self.text
font_size: 32
CustButton:
text: '÷'
on_press: entry.text += '/'
CustButton:
text: '×'
on_press: entry.text += '*'

RawLayout:
rows: 1
orientation: 'horizontal'
CustButton:
text: '7'
on_press: entry.text += self.text
CustButton:
text: '8'
on_press: entry.text += self.text
CustButton:
text: '9'
on_press: entry.text += self.text
CustButton:
text: '-'
on_press: entry.text += self.text

RawLayout:
orientation: 'horizontal'
rows: 1
CustButton:
text: '4'
on_press: entry.text += self.text
CustButton:
text: '5'
on_press: entry.text += self.text
CustButton:
text: '6'
on_press: entry.text += self.text
CustButton:
text: '+'
on_press: entry.text += self.text

RawLayout:
orientation: 'horizontal'
cols: 3
CustButton:
text: '1'
size_hint: [.5, 1]
on_press: entry.text += self.text
CustButton:
text: '2'
size_hint: [.5, 1]
on_press: entry.text += self.text
CustButton:
text: '3'
size_hint: [.5, 1]
on_press: entry.text += self.text
CustButton:
text: ' '
size_hint: [.5, 1]
background_normal: ' '
background_color: 0, 0, 0, 0

RawLayout:
orientation: 'horizontal'
size_hint: [1, .2]
CustButton:
text: '0'
on_press: entry.text += self.text
size_hint: [.34, 1]
CustButton:
text: '.'
on_press: entry.text += self.text
size_hint: [.17, 1]
font_size: 32
CustButton:
text: '='
on_press: calculator.calculate(entry.text)
size_hint: [.17, 2.4]
# background_normal: ' '
# background_color: 0, .5, 95, 1

最佳答案

Kivy ScreenManager

以下步骤说明如何使用 ScreenManager 扩展 Kivy 应用程序, Screen , 和 Button小部件,以及 Button 之一的事件(on_releaseon_press)。

Py 文件

  1. 添加导入语句,from kivy.uix.screenmanager import ScreenManager, Screen
  2. 声明一个继承 ScreenManager 的类,例如class ScreenManagement(ScreenManager):
  3. 声明两个继承 Screen 的类,例如class MenuScreen(Screen):class CalculatorScreen(Screen):
  4. 添加 pass作为三个新类的主体,因为我们将使用 kv 语言来设计它们的 View /演示。
  5. 替换return RootWidget()return ScreenManagement()因为现在 App 的根是 Kivy ScreenManager
  6. 重命名 class RootWidgetclass Calculator

片段 - py

from kivy.uix.screenmanager import ScreenManager, Screen

...

class Calculator(GridLayout):
...


class MenuScreen(Screen):
pass


class CalculatorScreen(Screen):
pass


class ScreenManagement(ScreenManager):
pass


class kutuApp(App):
def build(self):
return ScreenManagement()

kv 文件

  1. 声明 class rules , <MenuScreen>: , <CalculatorScreen>: <ScreenManagement>:对应于 class MenuScreen(Screen): , class CalculatorScreen(Screen): , 和 class ScreenManagement(ScreenManager):分别在 Python 脚本中
  2. 重命名 RootWidgetCalculator
  3. 实例化 Calculator:作为类(class)规则的 child ,<CalculatorScreen>:
  4. 实例化 MenuScreen:CalculatorScreen:作为类(Class)统治的 child ,<ScreenManagement>:
  5. nameMenuScreen:CalculatorScreen:作为 name: 'menu'name: 'calculator'分别。这将使我们能够在切换屏幕时引用它们。

片段 - kv

<ScreenManagement>:
MenuScreen:
name: 'menu'
CalculatorScreen:
name: 'calculator'

<MenuScreen>:
BoxLayout:
Button:
text: 'Goto Calculator'
on_press: root.manager.current = 'calculator'
Button:
text: 'Quit'

<CalculatorScreen>:
Calculator:

...
<Calculator>:
id: calculator

示例

main.py
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.core.window import Window
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen


Window.clearcolor = .3, .3, .3, 1


class Calculator(GridLayout):
def calculate(self, calculation):
if calculation:
try:
self.display.text = str(eval(calculation))
except Exception:
self.display.text = "Error"


class MenuScreen(Screen):
pass


class CalculatorScreen(Screen):
pass


class ScreenManagement(ScreenManager):
pass


Builder.load_file("main.kv")


class kutuApp(App):
def build(self):
return ScreenManagement()


if __name__ == '__main__':
kutuApp().run()

main.kv

<ScreenManagement>:
MenuScreen:
name: 'menu'
CalculatorScreen:
name: 'calculator'

<MenuScreen>:
BoxLayout:
Button:
text: 'Goto Calculator'
on_press: root.manager.current = 'calculator'
Button:
text: 'Quit'

<CalculatorScreen>:
Calculator:

<CustButton@Button>:
font_size: 35
background_color: 0,0,0,0
canvas.before:
Color:
rgba: (.4, .4, .4, 1) if self.state=='normal' else (0,.7,.7,1)
RoundedRectangle:
pos: self.pos
size: self.size
radius: [20, ]

<RawLayout@BoxLayout>:
spacing: 8
padding: 8
size_hint: [1, .2]

<Calculator>:
id: calculator
rows: 10
display: entry
spacing: 1

BoxLayout:
size_hint: [1, .1]
Label:
text: 'Basic Calculator'
Label:
text: 'Made by Xrew'

BoxLayout:
padding: 10
TextInput:
id: entry
spacing: 1
padding: 5
font_size: 32
multiline: True
focus: False
# background_color: 0, 0, 0, 1
# foreground_color: [1, 0, 1, 1]

RawLayout:
CustButton:
text: '<'
on_press: entry.text += self.text
CustButton:
text: '>'
on_press: entry.text += self.text
CustButton:
text: '≈'
on_press: entry.text += '=='

RawLayout:
orientation: 'horizontal'
CustButton:
text: '('
on_press: entry.text += '('
CustButton:
text: ')'
on_press: entry.text += ')'
CustButton:
text: '√'
on_press: entry.text += '**(.5)'
CustButton:
text: '¹/x'
on_press: entry.text += '1/'
RawLayout:
orientation: 'horizontal'
CustButton:
text: 'Del'
on_press: entry.text = entry.text[:-1]
CustButton:
text: 'x²'
on_press: entry.text += '**2'
CustButton:
text: 'xⁿ'
on_press: entry.text += '**'
CustButton:
text: 'π'
on_press: entry.text += '3.14'

RawLayout:
orientation: 'horizontal'
cols: 4
CustButton:
text: 'Clr'
font_color: [255,0,0,1]
# background_normal: ' '
# background_color: 1, .3, .4, .85
on_press: entry.text = ""
CustButton:
text: '+'
on_press: entry.text += self.text
font_size: 32
CustButton:
text: '÷'
on_press: entry.text += '/'
CustButton:
text: '×'
on_press: entry.text += '*'

RawLayout:
rows: 1
orientation: 'horizontal'
CustButton:
text: '7'
on_press: entry.text += self.text
CustButton:
text: '8'
on_press: entry.text += self.text
CustButton:
text: '9'
on_press: entry.text += self.text
CustButton:
text: '-'
on_press: entry.text += self.text

RawLayout:
orientation: 'horizontal'
rows: 1
CustButton:
text: '4'
on_press: entry.text += self.text
CustButton:
text: '5'
on_press: entry.text += self.text
CustButton:
text: '6'
on_press: entry.text += self.text
CustButton:
text: '+'
on_press: entry.text += self.text

RawLayout:
orientation: 'horizontal'
cols: 3
CustButton:
text: '1'
size_hint: [.5, 1]
on_press: entry.text += self.text
CustButton:
text: '2'
size_hint: [.5, 1]
on_press: entry.text += self.text
CustButton:
text: '3'
size_hint: [.5, 1]
on_press: entry.text += self.text
CustButton:
text: ' '
size_hint: [.5, 1]
background_normal: ' '
background_color: 0, 0, 0, 0

RawLayout:
orientation: 'horizontal'
size_hint: [1, .2]
CustButton:
text: '0'
on_press: entry.text += self.text
size_hint: [.34, 1]
CustButton:
text: '.'
on_press: entry.text += self.text
size_hint: [.17, 1]
font_size: 32
CustButton:
text: '='
on_press: calculator.calculate(entry.text)
size_hint: [.17, 2.4]
# background_normal: ' '
# background_color: 0, .5, 95, 1

输出

MenuScreen CalculatorScreen

关于python - Kivy多屏管理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56134865/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com