我在构建 exe 文件时遇到问题,其中 MDFloatButton 禁用在构建 exe 之后不起作用,但在构建之前可以正常工作。
for btn in self.ids.values(): # Loop through all widgets in self.ids
if isinstance(btn, MDFloatingActionButton) and btn != button_instance:
btn.disabled = True
这是单击后禁用按钮的代码片段,但是当我构建时,此功能不起作用,单击后它不会禁用按钮。
我正在使用:
block_cipher = None
from kivy_deps import sdl2, glew
from kivymd import hooks_path as kivymd_hooks_path
a = Analysis(
['Main.py'],
pathex=[],
binaries=[],
datas=[('login.kv', '.')],
hiddenimports=['kivymd.icon_definitions.icon_definitions', 'kivymd.uix.button', 'kivy.properties'],
hookspath=[kivymd_hooks_path],
hooksconfig={},
runtime_hooks=[],
excludes=[],
cipher=block_cipher,
noarchive=False,
optimize=0,
)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(
pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
*[Tree(p) for p in (sdl2.dep_bins + glew.dep_bins)],
name='Main',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
upx_exclude=[],
runtime_tmpdir=None,
console=True,
disable_windowed_traceback=False,
argv_emulation=False,
target_arch=None,
codesign_identity=None,
entitlements_file=None,
)
原因是因为类是一个按钮,代码中:
for btn in self.ids.values(): # Loop through all widgets in self.ids
if isinstance(btn, MDFloatingActionButton) and btn != button_instance:
btn.disabled = True
名称 btn 正在获取小部件的 id,然后分配 disabled=True 值,但是此时,在 kivymd 生命周期中,按钮正在接收 kivymd GUI 之外的值,以确保按钮小部件可以接受这个值并在 GUI 中表示它,你可以这样做:
在应用程序中定义一个函数:
def enable_disable_the_button(self,tell_me_the_widget,field1,field2):
the_button_is=tell_me_the_widget #this one comes from the GUI, gets stored here, meaning this variable is connected to the GUI, if I apply a change in its attributes it should work:
#I want this button to:
if len(field1.text)>0 and len(field2.text)>0:
the_button_is.disabled=False
return 0
if len(field1.text)==0 or len(field2.text)==0:
the_button_is.disabled=True
return 0
稍后您可以使用它或从 Kivy Lang 实现它:
Screen:
ScreenManager:
id: MainActivity
Screen:
id: SecondActivity
name: "main"
MDCard:
size_hint: None, None
size: 300, 400
pos_hint: {"center_x": 0.5, "center_y": 0.5}
elevation: 10
padding: 25
spacing: 25
orientation: 'vertical'
MDLabel:
id: welcome_label
text: "This is MJ"
font_size: 30
halign: 'center'
size_hint_y: None
height: self.texture_size[1]
padding_y: 15
MDTextFieldRound:
id: user
hint_text: "username"
icon_right: "account"
size_hint_x: None
width: 200
font_size: 18
pos_hint: {"center_x": 0.5}
on_text: app.enable_disable_the_button(root.ids.floatingbutton2024,root.ids.user,root.ids.password)
MDTextFieldRound:
id: password
hint_text: "password"
icon_right: "eye-off"
size_hint_x: None
width: 200
font_size: 18
pos_hint: {"center_x": 0.5}
password: True
on_text: app.enable_disable_the_button(root.ids.floatingbutton2024,root.ids.user,root.ids.password)
MDFloatingActionButton:
id: floatingbutton2024
icon: "login"
md_bg_color: [0,0,.5,1]
md_text_color: [1,1,1,1]
pos_hint: {"center_x": 0.5}
disabled: True
MDRoundFlatButton:
text: "Clear fields"
font_size: 12
pos_hint: {"center_x": 0.5}