将值从类获取到其他Python
问题描述
我正在使用Kivy为我的密码管理器制作图形界面。 我已经做好登录页了。我不知道如何将密码登录名上载到其他文件中的其他类以解密其他密码?
class MainApp(MDApp):
def build(self):
self.theme_cls.theme_style = "Dark"
self.theme_cls.primary_palette = "BlueGray"
return Builder.load_file('libskvlogin_page.kv')
def logger(self):
try :
passwd = verif_password(self.root.ids.password.text)
passwd_bdd = passwd
if passwd == True :
self.get_running_app().stop()
import graphical_password
else :
print("NO")
except :
print("No passs")
MainApp().run()
我的导入Graphical_Password是此代码:
class MainApp(MDApp):
def build(self):
screen = Screen()
table = MDDataTable(
pos_hint = {'center_x' : 0.5, 'center_y' : 0.5},
size_hint = (0.9, 0.6),
check = True,
column_data =[
("F_name", dp(30)),
("Name", dp(30)),
("Mail", dp(30)),
("Phone", dp(30)),
],
row_data = [
("P1","P2","GAMIL","0658"),
("P3","P4","GAMIL","0658"),
]
)
self.theme_cls.theme_style = "Light"
self.theme_cls.primary_palette = "BlueGray"
screen.add_widget(table)
return screen
MainApp().run()
您知道如何将第一个页面中输入的密码输入到另一个页面中吗?
解决方案
使用:
main.py
from kivymd.app import MDApp
from graphical_password import Screen_jbsidis
from graphical_password import *
from kivy.lang import Builder
from kivy.clock import Clock
def verif_password(a): return True
class MainAppjbsidis(MDApp):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.passs=""
self.user=""
self.mainx_jbsidis=Builder.load_file('libs/kv/login_page.kv')
def build(self):
global passs
global user
self.theme_cls.theme_style = "Dark"
self.theme_cls.primary_palette = "BlueGray"
return self.mainx_jbsidis
def logger(self,user,pasword_jbsidis):
try:
print(user.text,pasword_jbsidis.text)
self.passs=pasword_jbsidis.text
self.user=user.text
passwd = verif_password(pasword_jbsidis.text)
passwd_bdd = passwd
if passwd == True :
jbsidis="self.get_running_app().stop() #this is an issue because this may kill the thread and not execute the next one, plus it may use additional unnecessary memory"
jbsidis5="import graphical_password "
jbsidis6="self.mainx_jbsidis.ids.jbsidisBody.add_widget(Screen_jbsidis())"
self.mainx_jbsidis.ids.jbsidisBody.current="dos"
else :
print("NO")
except Exception as e:
print("No passs",str(e))
def put_data(self,TABLE):
kar="""
TABLE.data=[]
new_datax=[
{'text': self.passs, 'Index': 'P1', 'range': [0, 3], 'selectable': True, 'viewclass': 'CellRow', 'table': TableData},
{'text': self.user, 'Index': 'P1', 'range': [0, 3], 'selectable': True, 'viewclass': 'CellRow', 'table': TableData},
{'text': 'jbsidis', 'Index': 'P1', 'range': [0, 3], 'selectable': True, 'viewclass': 'CellRow', 'table': TableData},
{'text': '8822', 'Index': 'P1', 'range': [0, 3], 'selectable': True, 'viewclass': 'CellRow', 'table': TableData},
{'text': 'MyOtherPassword', 'Index': 'P3', 'range': [4, 7], 'selectable': True, 'viewclass': 'CellRow', 'table': TableData},
{'text': 'MyNameA', 'Index': 'P3', 'range': [4, 7], 'selectable': True, 'viewclass': 'CellRow', 'table': TableData},
{'text': 'jbsidis555', 'Index': 'P3', 'range': [4, 7], 'selectable': True, 'viewclass': 'CellRow', 'table': TableData},
{'text': '2222', 'Index': 'P3', 'range': [4, 7], 'selectable': True, 'viewclass': 'CellRow', 'table': TableData}
]
TABLE.data.append(new_datax)
TABLE.table_data.recycle_data=[]
row_dataZ = [
(self.passs,self.user,"jbsidis","1111"),
("AnotherPASS","OtherUser","jbsidis767","23232"),
]
TABLE.table_data=TableData(
TABLE.header,
row_data=row_dataZ,
check=True,
rows_num=2,
_parent=TABLE,
)
"""
self.mainx_jbsidis.ids.TABLE_EXTRA_jbsidis.clear_widgets()
self.table_refresher_jbsidis()
def table_refresher_jbsidis(self):
self.tablejbsidis = MDDataTable(
pos_hint = {'center_x' : 0.5, 'center_y' : 0.5},
size_hint = (0.9, 0.6),
check = True,
column_data =[
("Password", dp(30)),
("User", dp(30)),
("Mail", dp(30)),
("Phone", dp(30)),
],
row_data = [
(self.passs,self.user,"jbsidis","062328"),
("Otherpass","OtherUser","jbsidis222","2222"),
]
)
self.mainx_jbsidis.ids.TABLE_EXTRA_jbsidis.add_widget(self.tablejbsidis)
def constant_checker(self):
if self.passs!="" and self.user!="":
T=self.mainx_jbsidis.ids.TABLE_EXTRA_jbsidis.children
jbsidis223="print(T)"
jbsidis55333="[<graphical_password.MDDataTable object at 0x7f5a5dfe8740>]"
new_data= [
(self.passs,self.user,"jbsidis","06655")
,(self.passs,self.user,"jbsidis2","06655")]
Clock.schedule_once(lambda x:self.put_data(T[0]),3)
MainAppjbsidis().run()
libs/kv/login_page.kv
#:import Screen_jbsidis graphical_password.Screen_jbsidis
Screen:
ScreenManager:
id: jbsidisBody
Screen:
name: "uno_jbsidis"
FloatLayout:
BoxLayout:
orientation: "vertical"
padding: dp(30)
BoxLayout:
BoxLayout:
MDTextField:
id: jbsidis1
pos_hint: {"center_x": .5, "center_y": .5}
hint_text: "User"
MDTextField:
id: jbsidis2
pos_hint: {"center_x": .5, "center_y": .5}
hint_text: "Password"
MDTextButton:
id: jbsidis3
pos_hint: {"center_x": .5, "center_y": .5}
text: "Forgot password?"
MDFillRoundFlatButton:
id: jbsidis4
pos_hint: {"center_x": .5, "center_y": .5}
text: "Login"
on_release:
app.logger(root.ids.jbsidis1,root.ids.jbsidis2)
app.constant_checker()
BoxLayout:
BoxLayout:
Screen_jbsidis:
id: TABLE_EXTRA_jbsidis
name: "dos"
Graphical_password.py
from kivy.uix.screenmanager import Screen
from kivy.lang import Builder
from kivy import Logger
from kivy.metrics import dp
from kivy.properties import (
BooleanProperty,
ListProperty,
ObjectProperty,
NumericProperty,
DictProperty,
StringProperty,
OptionProperty,
)
from kivy.clock import Clock
from kivy.uix.scrollview import ScrollView
from kivy.uix.behaviors import FocusBehavior, ButtonBehavior
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.recycleview import RecycleView
from kivy.uix.recyclegridlayout import RecycleGridLayout
from kivy.uix.recycleview.layout import LayoutSelectionBehavior
from kivy.uix.recycleview.views import RecycleDataViewBehavior
from kivymd.theming import ThemableBehavior
from kivymd.uix.behaviors import HoverBehavior
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.uix.dialog import BaseDialog
from kivymd.uix.tooltip import MDTooltip
from kivymd.uix.menu import MDDropdownMenu
Builder.load_string(
"""
#:import DEVICE_TYPE kivymd.material_resources.DEVICE_TYPE
<CellRow>
orientation: "vertical"
canvas.before:
Color:
rgba:
(root.theme_cls.bg_darkest if root.theme_cls.theme_style == "Light" else root.theme_cls.bg_light)
if self.selected else root.theme_cls.bg_normal
Rectangle:
pos: self.pos
size: self.size
on_press: if DEVICE_TYPE != "desktop": root.table.on_mouse_select(self)
on_enter: if DEVICE_TYPE == "desktop": root.table.on_mouse_select(self)
MDBoxLayout:
id: box
padding: "8dp", "8dp", 0, "8dp"
spacing: "16dp"
MDCheckbox:
id: check
size_hint: None, None
size: 0, 0
opacity: 0
on_active: root.select_check(self.active)
MDLabel:
id: label
text: " " + root.text
color: (1, 1, 1, 1) if root.theme_cls.theme_style == "Dark" else (0, 0, 0, 1)
<CellHeader>
orientation: "vertical"
size_hint_y: None
height: self.minimum_height
spacing: "4dp"
tooltip_text: root.text
MDLabel:
text: " " + root.text
size_hint_y: None
height: self.texture_size[1]
bold: True
color: (1, 1, 1, 1) if root.theme_cls.theme_style == "Dark" else (0, 0, 0, 1)
MDSeparator:
id: separator
<TableHeader>
bar_width: 0
do_scroll: False
size_hint: 1, None
height: header.height
MDGridLayout:
id: header
rows: 1
cols_minimum: root.cols_minimum
adaptive_size: True
padding: 0, "8dp", 0, 0
MDBoxLayout:
orientation: "vertical"
MDBoxLayout:
id: box
padding: "8dp", "8dp", "4dp", 0
spacing: "16dp"
MDCheckbox:
id: check
size_hint: None, None
size: 0, 0
opacity: 0
on_active: root.table_data.select_all(self.state)
disabled: True
CellHeader:
id: first_cell
MDSeparator:
<TableData>
data: root.recycle_data
data_first_cells: root.data_first_cells
key_viewclass: "viewclass"
TableRecycleGridLayout:
id: row_controller
key_selection: "selectable"
cols: root.total_col_headings
cols_minimum: root.cols_minimum
default_size: None, dp(52)
default_size_hint: 1, None
size_hint: None, None
height: self.minimum_height
width: self.minimum_width
orientation: 'lr-tb'
multiselect: True
touch_multiselect: True
<TablePagination>
adaptive_height: True
spacing: "8dp"
Widget:
MDLabel:
text: "Rows per page"
size_hint: None, 1
width: self.texture_size[0]
text_size: None, None
font_style: "Caption"
color: (1, 1, 1, 1) if root.theme_cls.theme_style == "Dark" else (0, 0, 0, 1)
MDDropDownItem:
id: drop_item
pos_hint: {'center_y': .5}
text: str(root.table_data.rows_num)
font_size: "14sp"
on_release: root.table_data.open_pagination_menu()
Widget:
size_hint_x: None
width: "32dp"
MDLabel:
id: label_rows_per_page
text: f"1-{root.table_data.rows_num} of {len(root.table_data.row_data)}"
size_hint: None, 1
#width: self.texture_size[0]
text_size: None, None
font_style: "Caption"
color: (1, 1, 1, 1) if root.theme_cls.theme_style == "Dark" else (0, 0, 0, 1)
MDIconButton:
id: button_back
icon: "chevron-left"
user_font_size: "20sp"
pos_hint: {'center_y': .5}
disabled: True
on_release: root.table_data.set_next_row_data_parts("back")
MDIconButton:
id: button_forward
icon: "chevron-right"
user_font_size: "20sp"
pos_hint: {'center_y': .5}
on_release: root.table_data.set_next_row_data_parts("forward")
<MDDataTable>
MDCard:
id: container
orientation: "vertical"
elevation: 14
md_bg_color: 0, 0, 0, 0
padding: "24dp", "24dp", "8dp", "8dp"
canvas:
Color:
rgba: root.theme_cls.bg_normal
RoundedRectangle:
pos: self.pos
size: self.size
""")
class TableRecycleGridLayout(
FocusBehavior, LayoutSelectionBehavior, RecycleGridLayout
):
selected_row = NumericProperty(0)
table_data = ObjectProperty(None)
def get_nodes(self):
nodes = self.get_selectable_nodes()
if self.nodes_order_reversed:
nodes = nodes[::-1]
if not nodes:
return None, None
selected = self.selected_nodes
if not selected:
self.selected_row = 0
self.select_row(nodes)
return None, None
if len(nodes) == 1:
return None, None
index = selected[-1]
if index > len(nodes):
last = len(nodes)
else:
last = nodes.index(index)
self.clear_selection()
return last, nodes
def select_next(self, instance):
"""Select next row."""
self.table_data = instance
last, nodes = self.get_nodes()
if not nodes:
return
if last == len(nodes) - 1:
self.selected_row = nodes[0]
else:
self.selected_row = nodes[last + 1]
self.selected_row += self.table_data.total_col_headings
self.select_row(nodes)
def select_current(self, instance):
self.table_data = instance
last, nodes = self.get_nodes()
if not nodes:
return
self.select_row(nodes)
def select_row(self, nodes):
col = self.table_data.recycle_data[self.selected_row]["range"]
for x in range(col[0], col[1] + 1):
self.select_node(nodes[x])
class CellRow(
ThemableBehavior,
RecycleDataViewBehavior,
HoverBehavior,
ButtonBehavior,
BoxLayout,
):
text = StringProperty()
table = ObjectProperty()
index = None
selected = BooleanProperty(False)
selectable = BooleanProperty(True)
def on_table(self, instance, table):
if not table.check:
self.ids.box.padding = 0
self.ids.box.spacing = 0
def refresh_view_attrs(self, table_data, index, data):
self.index = index
return super().refresh_view_attrs(table_data, index, data)
def on_touch_down(self, touch):
if super().on_touch_down(touch):
if self.table._parent:
self.table._parent.dispatch("on_row_press", self)
return True
def apply_selection(self, table_data, index, is_selected):
self.selected = is_selected
if table_data.check:
if self.text in table_data.data_first_cells:
self.ids.check.size = (dp(32), dp(32))
self.ids.check.opacity = 1
self.ids.box.spacing = dp(16)
self.ids.box.padding[0] = dp(8)
else:
self.ids.check.size = (0, 0)
self.ids.check.opacity = 0
self.ids.box.spacing = 0
self.ids.box.padding[0] = 0
if table_data._rows_number in table_data.current_selection_check:
for index in table_data.current_selection_check[
table_data._rows_number
]:
if (
self.index
in table_data.current_selection_check[
table_data._rows_number
]
):
self.ids.check.state = "down"
else:
self.ids.check.state = "normal"
else:
self.ids.check.state = "normal"
def select_check(self, active):
if active and self.index not in self.table.current_selection_check:
if (
not self.table._rows_number
in self.table.current_selection_check
):
self.table.current_selection_check[self.table._rows_number] = []
if (
self.index
not in self.table.current_selection_check[
self.table._rows_number
]
):
self.table.current_selection_check[
self.table._rows_number
].append(self.index)
else:
if self.table._rows_number in self.table.current_selection_check:
if (
self.index
in self.table.current_selection_check[
self.table._rows_number
]
and not active
):
self.table.current_selection_check[
self.table._rows_number
].remove(self.index)
self.table.get_select_row(self.index)
class CellHeader(MDTooltip, BoxLayout):
text = StringProperty()
class TableHeader(ScrollView):
table_data = ObjectProperty()
column_data = ListProperty()
col_headings = ListProperty()
sort = BooleanProperty(False)
cols_minimum = DictProperty()
def __init__(self, **kwargs):
super().__init__(**kwargs)
for i, col_heading in enumerate(self.column_data):
self.cols_minimum[i] = col_heading[1] * 5
self.col_headings.append(col_heading[0])
if i:
self.ids.header.add_widget(
CellHeader(text=col_heading[0], width=self.cols_minimum[i])
)
else:
self.ids.first_cell.text = col_heading[0]
self.ids.first_cell.ids.separator.height = 0
self.ids.first_cell.width = self.cols_minimum[i]
def on_table_data(self, instance, value):
if self.table_data.check:
self.ids.check.size = (dp(32), dp(32))
self.ids.check.opacity = 1
else:
self.ids.box.padding[0] = 0
self.ids.box.spacing = 0
def on_sort(self, instance, value):
Logger.info("TableData: Sorting table items is not implemented")
class TableData(RecycleView):
recycle_data = ListProperty()
data_first_cells = ListProperty()
row_data = ListProperty()
total_col_headings = NumericProperty(0)
cols_minimum = DictProperty()
table_header = ObjectProperty()
pagination_menu = ObjectProperty()
pagination = ObjectProperty()
check = ObjectProperty()
rows_num = NumericProperty()
pagination_menu_open = BooleanProperty(False)
current_selection_check = DictProperty()
sort = BooleanProperty()
_parent = ObjectProperty()
_rows_number = NumericProperty(0)
_rows_num = NumericProperty()
_current_value = NumericProperty(1)
_to_value = NumericProperty()
_row_data_parts = ListProperty()
def __init__(self, table_header, **kwargs):
super().__init__(**kwargs)
self.table_header = table_header
self.total_col_headings = len(table_header.col_headings)
self.cols_minimum = table_header.cols_minimum
self.set_row_data()
Clock.schedule_once(self.set_default_first_row, 0)
def get_select_row(self, index):
row = []
for data in self.recycle_data:
if index in data["range"]:
row.append(data["text"])
self._parent.dispatch("on_check_press", row)
def set_default_first_row(self, dt):
self.ids.row_controller.select_next(self)
def sort_by_name(self):
print(1)
def set_row_data(self):
data = []
low = 0
high = self.total_col_headings - 1
self.recycle_data = []
self.data_first_cells = []
if self._row_data_parts:
for row in self._row_data_parts[self._rows_number]:
for i in range(len(row)):
data.append([row[i], row[0], [low, high]])
low += self.total_col_headings
high += self.total_col_headings
for j, x in enumerate(data):
if x[0] == x[1]:
self.data_first_cells.append(x[0])
self.recycle_data.append({"text": str(x[0]),"Index": str(x[1]),"range": x[2],"selectable": True,"viewclass": "CellRow","table": self})
if not self.table_header.column_data:
raise ValueError(f"Set value for column_data in class TableData")
self.data_first_cells.append(self.table_header.column_data[0][0])
def set_text_from_of(self, direction):
if self.pagination:
if direction == "forward":
if (len(self._row_data_parts[self._rows_number])< self._to_value):
self._current_value = self._current_value + self.rows_num
else:
self._current_value = self._current_value + len(self._row_data_parts[self._rows_number])
self._to_value = self._to_value + len(self._row_data_parts[self._rows_number])
if direction == "back":
self._current_value = self._current_value - len(self._row_data_parts[self._rows_number])
self._to_value = self._to_value - len(self._row_data_parts[self._rows_number])
if direction == "increment":
self._current_value = 1
self._to_value = self.rows_num + self._current_value - 1
self.pagination.ids.label_rows_per_page.text = f"{self._current_value}-{self._to_value} of {len(self.row_data)}"
def select_all(self, state):
for i, data in enumerate(self.recycle_data):
opts = self.layout_manager.view_opts
cell_row_obj = self.view_adapter.get_view(i, self.data[i], opts[i]["viewclass"])
cell_row_obj.ids.check.state = state
self.on_mouse_select(cell_row_obj)
cell_row_obj.select_check(True if state == "down" else False)
def close_pagination_menu(self, *args):
self.pagination_menu_open = False
def open_pagination_menu(self):
if self.pagination_menu.items:
self.pagination_menu_open = True
self.pagination_menu.open()
def set_number_displayed_lines(self, instance_menu_item):
self.rows_num = int(instance_menu_item.text)
self.set_row_data()
self.set_text_from_of("increment")
def set_next_row_data_parts(self, direction):
if direction == "forward":
self._rows_number += 1
self.pagination.ids.button_back.disabled = False
elif direction == "back":
self._rows_number -= 1
self.pagination.ids.button_forward.disabled = False
self.set_row_data()
self.set_text_from_of(direction)
if self._to_value == len(self.row_data):
self.pagination.ids.button_forward.disabled = True
if self._current_value == 1:
self.pagination.ids.button_back.disabled = True
def _split_list_into_equal_parts(self, lst, parts):
for i in range(0, len(lst), parts):
yield lst[i : i + parts]
def on_mouse_select(self, instance):
if not self.pagination_menu_open:
if self.ids.row_controller.selected_row != instance.index:
self.ids.row_controller.selected_row = instance.index
self.ids.row_controller.select_current(self)
def on_rows_num(self, instance, value):
if not self._to_value:
self._to_value = value
self._rows_number = 0
self._row_data_parts = list(
self._split_list_into_equal_parts(self.row_data, value)
)
class TablePagination(ThemableBehavior, MDBoxLayout):
table_data = ObjectProperty()
class MDDataTable(BaseDialog):
column_data = ListProperty()
row_data = ListProperty()
sort = BooleanProperty(False)
check = BooleanProperty(False)
use_pagination = BooleanProperty(False)
rows_num = NumericProperty(5)
pagination_menu_pos = OptionProperty("center", options=["center", "auto"])
pagination_menu_height = NumericProperty("140dp")
background_color = ListProperty([0, 0, 0, 0])
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.register_event_type("on_row_press")
self.register_event_type("on_check_press")
self.header = TableHeader(column_data=self.column_data, sort=self.sort)
self.table_data = TableData(
self.header,
row_data=self.row_data,
check=self.check,
rows_num=self.rows_num,
_parent=self,
)
self.pagination = TablePagination(table_data=self.table_data)
self.table_data.pagination = self.pagination
self.header.table_data = self.table_data
self.table_data.fbind("scroll_x", self._scroll_with_header)
self.ids.container.add_widget(self.header)
self.ids.container.add_widget(self.table_data)
if self.use_pagination:
self.ids.container.add_widget(self.pagination)
Clock.schedule_once(self.create_pagination_menu, 0.5)
def on_row_press(self, *args):
"""Called when a table row is clicked."""
def on_check_press(self, *args):
"""Called when the check box in the table row is checked."""
def _scroll_with_header(self, instance, value):
self.header.scroll_x = value
def create_pagination_menu(self, interval):
menu_items = [
{"text": f"{i}"}
for i in range(
self.rows_num, len(self.row_data) // 2, self.rows_num,
)
]
pagination_menu = MDDropdownMenu(
caller=self.pagination.ids.drop_item,
items=menu_items,
use_icon_item=False,
position=self.pagination_menu_pos,
max_height=self.pagination_menu_height,
callback=self.table_data.set_number_displayed_lines,
width_mult=2,
)
pagination_menu.bind(on_dismiss=self.table_data.close_pagination_menu)
self.table_data.pagination_menu = pagination_menu
from kivy.metrics import dp
class Screen_jbsidis(Screen):
def __init__(self, **kwargs):
super(Screen_jbsidis, self).__init__(**kwargs)
ii="screen = Screen()"
self.table = MDDataTable(
pos_hint = {'center_x' : 0.5, 'center_y' : 0.5},
size_hint = (0.9, 0.6),
check = True,
column_data =[
("F_name", dp(30)),
("Name", dp(30)),
("Mail", dp(30)),
("Phone", dp(30)),
],
row_data = [
("P1","P2","GAMIL","0658"),
("P3","P4","GAMIL","0658"),
]
)
self.add_widget(self.table)
#return Screen_jbsidis
m="MainApp().run()"
演示其工作原理的视频:https://youtu.be/84wpXlCaTDM
Pics,jbsidis:
相关文章