我正在制作一個簡單的應用程式,當用戶按下特定按鈕時,它會為用戶提供有關特定飛機警告/警告的深入資訊。我很新,這可能是一個非常基本的問題,但我根本不知道該怎么做。
我不確定組織文本資料的最佳方法是否是將每個警告/警告放在一個單獨的.py檔案中它自己的字串變數下,或者可能為每個警告/警告創建一個具有不同模塊的類,模塊只是字串資訊。也許是字典?也許最好的方法是將其作為文本檔案???
對于每個警告/警告,將有大約 200 個單詞描述與之相關的問題和解決方案。
我的意思的一個非常簡短的例子是:如果有人按下“電池”警告,按鈕會呼叫該get_text功能main.py并將按鈕的id. 在get_text使用了id已發送給它,然后從拉文本AOM.py或.txt或什么的。然后該資訊將顯示在頁面上,大約 200 字左右的內容是關于電池的問題是什么以及如何修復它。(在我的示例中,我故意忽略了更改螢屏的需要,因為我知道如何做到這一點)。我只是暫時離開print()。
如果有人可以幫助我,我在這里有一個可重現的最小示例?非常感謝。
主檔案
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen
import AOM
class CautionPanelScreen(Screen):
pass
class TextScreen(Screen):
pass
GUI = Builder.load_file("main.kv")
class MainApp(App):
def build(self):
return GUI
def change_screen(self):
# get the screen manager from the main.kv file
screen_manager = self.root.ids['screen_manager']
screen_manager.current = 'text_screen'
def get_text(self, caution):
print(AOM.caution) # this is where i am stuck, I dont know how to ref the data...
MainApp().run()
主檔案
#:include caution_panel_screen.kv
#:include text_screen.kv
GridLayout:
cols: 1
FloatLayout:
GridLayout:
canvas:
Color:
rgb: utils.get_color_from_hex("#EEEEEE")
Rectangle:
size: self.size
pos: self.pos
ScreenManager:
id: screen_manager
CautionPanelScreen:
name: "caution_panel_screen"
id: caution_panel_screen
care_panel_screen.kv
#:import utils kivy.utils
<CautionPanelScreen>:
FloatLayout:
flt_data_recorder: flt_data_recorder.__self__
canvas:
Color:
rgb: utils.get_color_from_hex("#000000")
Rectangle:
size: self.size
pos: self.pos
GridLayout:
rows: 1
id: row_1
pos_hint: {"top": .9, "left": 1}
size_hint: 1, .1
spacing: 5, 5
Button:
pos_hint: {"top": 1, "left": 1}
markup: True
opacity: 1 if self.state == 'normal' else .5
font_size: '12sp'
id: pri_inv
halign: 'center'
text: "[color=#FFF300]PRI INV[/color]"
on_release:
app.get_text(pri_inv)
Button:
pos_hint: {"top": 1, "left": 1}
markup: True
opacity: 1 if self.state == 'normal' else .5
font_size: '12sp'
id: l_ac_bus
halign: 'center'
text: "[color=#FFF300]L AC BUS[/color]"
on_release:
app.get_text(l_ac_bus)
Button:
pos_hint: {"top": 1, "left": 1}
markup: True
opacity: 1 if self.state == 'normal' else .5
font_size: '12sp'
id: emer_lts_disarmed
halign: 'center'
text: "[color=#FFF300]EMER LTS\nDISARMED[/color]"
on_release:
app.get_text(emer_lts_disarmed)
AOM檔案
#THIS IS JUST THE DATA PAGE AND CAN BE LAID OUT IN ANY WAY, dictionary, variables, txt, module, functions...
pri_inv = "all the things assosicated with primary inverter"
left_ac_bus = "all the stuff needed for info on left ac buses"
emer_lts_disarmed = "evertyhing on emergency lights"
EDIT: Will I have to store the text on my main.py? Or can I have it separate, like maybe in a word doc?
uj5u.com熱心網友回復:
我會保留它作為字典
AOM檔案
data = {
'pri_inv': "all the things assosicated with primary inverter",
'left_ac_bus': "all the stuff needed for info on left ac buses",
'emer_lts_disarmed': "evertyhing on emergency lights",
}
并且Button應該在輸入get_text()中使用字串on_release
on_release:
app.get_text("pri_inv")
on_release:
root.get_text("left_ac_bus")
on_release:
app.get_text("emer_lts_disarmed")
然后代碼應該使用這個字串來獲取文本 AOM.data
def get_text(self, caution):
print(AOM.data[caution])
也許以后我會用AOM.data生成,Button所以我也會保留按鈕的文本AOM.data
data = {
'pri_inv': ["PRI INV", "all the things assosicated with primary inverter"],
'left_ac_bus': ["L AC BUS", "all the stuff needed for info on left ac buses"],
'emer_lts_disarmed': ["EMER LTS\nDISARMED", "evertyhing on emergency lights"],
}
然后它需要使用索引[1]來獲取文本
def get_text(self, caution):
print(AOM.data[caution][1])
或者它可以保留在嵌套字典中
data = {
'pri_inv': {
"button_text": "PRI INV",
"text": "all the things assosicated with primary inverter",
},
'left_ac_bus': {
"button_text": "L AC BUS",
"text": "all the stuff needed for info on left ac buses",
},
'emer_lts_disarmed': {
"button_text": "EMER LTS\nDISARMED",
"text": "evertyhing on emergency lights",
},
}
然后它需要["text"]用來獲取文本
def get_text(self, caution):
print(AOM.data[caution]["text"])
如果按鈕有其他唯一值,那么我也會將它們保存在字典中。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/368303.html
標籤:python database string class text
上一篇:Java:替代String.contains可以回傳相似性
下一篇:將一串數字變成對應的字母
