gpt4 book ai didi

android - 在单个应用程序和 kivy 模拟器中使用 kivy 上的 2 个页面布局

转载 作者:行者123 更新时间:2023-11-29 18:43:10 24 4
gpt4 key购买 nike

我使用 Kivy 制作了一个简单的计算器应用程序。我面临的问题是我只能在应用程序中使用一种布局。我可以只使用网格布局,而我需要页面布局和网格布局。我想要一个 slider ,通过它我可以滚动并查看应用程序中的其余按钮,而不是一页上的所有按钮。更准确地说,代码是:

主要.py

from __future__ import division
import kivy
from math import sqrt
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.config import Config
from kivy.uix.pagelayout import PageLayout
class CalcGridLayout(GridLayout):
def something(x):
x+="hi"
return x
# Function called when equals is pressed
def calculate(self, calculation):
if calculation:
try:
# Solve formula and display it in entry
# which is pointed at by display
self.display.text = str(eval(calculation))
except Exception:
self.display.text = "Galat Hai Bhai !"
class CalculatorApp(App):
def build(self):
return CalcGridLayout()
calcApp = CalculatorApp()
calcApp.run()
#buildozer init
#buildozer android debug deploy ---------------code to deploy into apk and then transfer the file into ur mobile

计算器.kv:

# Custom button
<CustButton@Button>:
font_size: 65
#color:.25,.80,.92,1
size:100,100
background_color:.50,.50,.50,1

# Define id so I can refer to the CalcGridLayout
# class functions
# Display points to the entry widget
<CalcGridLayout>:
id: calculator
display: entry
rows: 8
padding: 0
spacing: 0

# Where input is displayed
BoxLayout:
TextInput:
id: entry
font_size: 80
multiline: False

# When buttons are pressed update the entry
BoxLayout:
spacing: 0
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

BoxLayout:
spacing: 0
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

BoxLayout:
spacing: 0
CustButton:
text: "1"
on_press: entry.text += self.text
CustButton:
text: "2"
on_press: entry.text += self.text
CustButton:
text: "3"
on_press: entry.text += self.text
CustButton:
text: "*"
on_press: entry.text += self.text

# When equals is pressed pass text in the entry
# to the calculate function
BoxLayout:
spacing: 0
CustButton:
text: "AC"
on_press: entry.text = ""
CustButton:
text: "0"
on_press: entry.text += self.text
CustButton:
text: "="
on_press: calculator.calculate(entry.text)
CustButton:
text: "/"
on_press: entry.text += self.text
#my new layout
BoxLayout:
spacing: 0
CustButton:
text: "Del"
on_press: entry.text =entry.text[:-1]
CustButton:
text: "Pow"
on_press: entry.text += '**'
CustButton:
text: "//"
on_press: entry.text +=self.text
CustButton:
text: "mod"
on_press: entry.text +='%'
BoxLayout:
CustButton:
text: "Made for learning face"

现在这个应用程序的输出非常简单: enter image description here

在这里所有的数字和操作都只在一页上。我想要另一个我可以滚动的页面,它必须包含像'(',')'这样的操作。我正在考虑为此实现页面布局,但无法在应用程序中同时实现布局(网格+页面)。有人可以帮忙吗?我的第二个疑问是我可以同时在移动设备(有点模拟器)上检查应用程序,我尝试使用 Kivy Remote Shell 但它没有用。每次我必须使用 buildozer init 然后部署它本身需要很多时间。然后用U盘把apk文件传到手机里测试。这很费时间。

最佳答案

你想使用 PageLayout 是对的可以做到,只需按照以下步骤操作:

第 1 步:在您的 main.py 中添加这一行:

from kivy.uix.pagelayout import PageLayout

然后在你的CalcGridLayout类继承自 PageLayout而不是 GridLayout像这样:

class CalcGridLayout(PageLayout):

步骤 02:在 <CalcGridLayout>: 之后的 calculator.kv 中添加以下内容:

GridLayout:

然后根据需要缩进您的代码。下面是一个工作示例..取自您的代码:ma​​in.py

from __future__ import division
import kivy
from math import sqrt
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.pagelayout import PageLayout
from kivy.config import Config
from kivy.uix.pagelayout import PageLayout

class CalcGridLayout(PageLayout):
def something(x):
x+="hi"
return x
# Function called when equals is pressed
def calculate(self, calculation):
if calculation:
try:
# Solve formula and display it in entry
# which is pointed at by display
self.display.text = str(eval(calculation))
except Exception:
self.display.text = "Galat Hai Bhai !"
class CalculatorApp(App):
def build(self):
return CalcGridLayout()
if __name__=='__main__':
calcApp = CalculatorApp()
calcApp.run()

和calculator.kv

# Custom button
<CustButton@Button>:
font_size: 65
#color:.25,.80,.92,1
size:100,100
background_color:.50,.50,.50,1

# Define id so I can refer to the CalcGridLayout
# class functions
# Display points to the entry widget
<CalcGridLayout>:
GridLayout:
id: calculator
display: entry
rows: 8
padding: 0
spacing: 0

# Where input is displayed
BoxLayout:
TextInput:
id: entry
font_size: 80
multiline: False

# When buttons are pressed update the entry
BoxLayout:
spacing: 0
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

BoxLayout:
spacing: 0
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

BoxLayout:
spacing: 0
CustButton:
text: "1"
on_press: entry.text += self.text
CustButton:
text: "2"
on_press: entry.text += self.text
CustButton:
text: "3"
on_press: entry.text += self.text
CustButton:
text: "*"
on_press: entry.text += self.text

# When equals is pressed pass text in the entry
# to the calculate function
BoxLayout:
spacing: 0
CustButton:
text: "AC"
on_press: entry.text = ""
CustButton:
text: "0"
on_press: entry.text += self.text
CustButton:
text: "="
on_press: calculator.calculate(entry.text)
CustButton:
text: "/"
on_press: entry.text += self.text
#my new layout
BoxLayout:
spacing: 0
CustButton:
text: "Del"
on_press: entry.text =entry.text[:-1]
CustButton:
text: "Pow"
on_press: entry.text += '**'
CustButton:
text: "//"
on_press: entry.text +=self.text
CustButton:
text: "mod"
on_press: entry.text +='%'
BoxLayout:
CustButton:
text: "Made for learning face"
BoxLayout:
id: test
Button:
text: 'You can then add more widgets here'

最后关于模拟器,这在 kivy 中还不是真正的模拟器,但是您可以通过将您的应用配置为在特定设备的规范上运行来模拟设备,例如运行您的应用模拟 Motorola droid 2:

KIVY_DPI=240 KIVY_METRICS_DENSITY=1.5 python main.py --size 854x480

或 HTC ONE X:

KIVY_DPI=320 KIVY_METRICS_DENSITY=2 python main.py --size 1280x720

或任何其他设备:

KIVY_DPI=<your-desired-dpi> KIVY_METRICS_DENSITY=<your-device-density> python main.py --size <your-device-size>

关于android - 在单个应用程序和 kivy 模拟器中使用 kivy 上的 2 个页面布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52580273/

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