[英]Kivy MDToolBar right_action_items: Can't change Screens
For the life of me I can't seem to figure out how to use the MDToolBar right_action_items:
in kv to change screens.对于我的生活,我似乎无法弄清楚如何使用 MDToolBar
right_action_items:
in kv 来改变屏幕。 I know that with a button on_release:
I can add a simple app.root.current = 'analyzer'
(I defined my screen as 'analyzer' and 'main').我知道使用
on_release:
我可以添加一个简单的app.root.current = 'analyzer'
(我将我的屏幕定义为 'analyzer' 和 'main')。
Yet you can't add this code to a right_action_items: Lambda x: app.root.current = 'analyzer'
in kv.但是您不能将此代码添加到
right_action_items: Lambda x: app.root.current = 'analyzer'
in kv。 So I tried adding a function in python that would do this for me inside my Manager(ScreenManager)
-->所以我尝试在 python 中添加一个 function ,它将在我的
Manager(ScreenManager)
中为我执行此操作 -->
def change_screen(self, inst):
Manager.current = f'{inst.ids.main.text}'
But it doesn't work... I'm lost, confused, afraid... help.但它不起作用......我迷路了,困惑,害怕......帮助。 me.
我。 please...
请...
main.py主文件
from kivymd.app import MDApp
from kivymd.theming import ThemeManager
from kivymd.uix.dialog import MDDialog
from kivy.uix.screenmanager import Screen, ScreenManager
from kivymd.uix.list import TwoLineListItem
from kivymd.uix.button import MDIconButton
from kivymd import *
from PyDictionary import PyDictionary
import sys
import json
import requests
class Manager(ScreenManager):
def change_screen(self, inst):
Manager.current = f'{inst.ids.main.text}'
class Main(Screen):
"""main application goes here"""
def close_dialog(self, obj):
self.dialog.dismiss()
def show_data(self):
message = """
Think of Probably Knot (PK) as a study
guide of sorts. Helping the user guide
himself.
This little program was designed to help
re-think ones sentences and therefore
find new solutions. By changing ones
perception, things can become more clear
where once they were misunderstood.
PK re-shuffle a word from an input
sentence to help rephrase ones
orignal sentence. To better
understand problems and ideas by
changing the angle of perception
with more elegant solutions.
"""
close = MDIconButton(icon="close-circle", on_release=self.close_dialog)
#more = MDIconButton(icon="more")
self.dialog = MDDialog(title="Probably Knot Helper", text=message,
size_hint=(0.8, 1), buttons=[close])
self.dialog.open()
class Analyzer(Screen):
def analyze(self, main): # main is pointing to ---> Main().show_data()
"""Analyse data with PyDictionary"""
sent = main.ids.sentence.text.lower()
wrd = main.ids.word.text.lower()
print(sent, wrd)
# Definition Section #
dictionary = PyDictionary()
define_wrd = dictionary.meaning(wrd)
if wrd != '' and sent != '':
API_KEY = 'a701e74e453ee6695e450310340401f5'
URL = f'http://words.bighugelabs.com/api/2/{API_KEY}/{wrd}/json'
if wrd not in sent:
print("i made it")
error = MDDialog(title="Error", text=f"Word: '{wrd}' is not in\n\n'{sent}'")
error.open()
else:
r = requests.get(URL) # get's url json file
j = json.loads(r.text) # loads json into 'j' as a dict
if type(j) == dict: # check is 'j' variable is coming in as a Dict holds the new sentences new = f"{result}\n"
final_set = set()
try:
for w in j['adjective']['syn']:
final_set.add(w)
except KeyError:
print(f'Adjective for "{wrd}" is not found.')
try:
for w in j['noun']['syn']:
final_set.add(w)
except KeyError:
print(f'Noun for "{wrd}" is not found.')
try:
for w in j['verb']['syn']:
final_set.add(w)
except KeyError:
print(f'Verb for "{wrd}" is not found.')
item = TwoLineListItem(text=f"Original: {sent}", secondary_text=f"{wrd}")
self.ids.container.add_widget(item)
for word in final_set:
item = TwoLineListItem(text=f"{sent.replace(wrd, word)}", secondary_text=f"{word}")
self.ids.container.add_widget(item)
# try:
# for num, w in enumerate(j['adjective']['syn'], 1):
# item = OneLineListItem(text=f"{num}: {sent.replace(wrd, w)}\n")
# self.ids.container.add_widget(item)
# except KeyError:
# print(f'Adjective for "{wrd}" is not found.')
# try:
# for num, w in enumerate(j['noun']['syn'], 1):
# item = OneLineListItem(text=f"{num}: {sent.replace(wrd, w)}\n")
# self.ids.container.add_widget(item)
# except KeyError:
# print(f'Noun for "{wrd}" is not found.')
# try:
# for num, w in enumerate(j['verb']['syn'], 1):
# item = OneLineListItem(text=f"{num}: {sent.replace(wrd, w)}\n")
# self.ids.container.add_widget(item)
# except KeyError:
# print(f'Verb for "{wrd}" is not found.')
class ProbablyKnotApp(MDApp):
def build(self):
self.theme_cls = ThemeManager()
self.theme_cls.theme_style = "Dark"
self.theme_cls.primary_palette = "Amber"
self.theme_cls.primary_hue = "A700"
return Manager()
if __name__ == "__main__":
ProbablyKnotApp().run()
kv千伏
<Manager>:
Main:
id: main
name: 'main'
Analyzer:
id: analyze
name: 'analyzer'
<Main>:
BoxLayout:
orientation: 'vertical'
MDToolbar:
id: toolbar
title: "Probably Knot v3"
md_bg_color: app.theme_cls.primary_color
right_action_items: [["help-circle-outline", lambda x: app.root.get_screen('main').show_data()]]
MDFloatLayout:
MDTextField:
id: sentence
icon_right: "book-open-outline"
icon_right_color: app.theme_cls.primary_color
hint_text: "Enter Sentence"
helper_text: "Write a problem statement to analyze"
helper_text_mode: "on_focus"
multiline: False
pos_hint: {'center_x': 0.5, 'center_y': 0.7}
size_hint_x: None
width: root.width - dp(20)
MDTextField:
id: word
icon_right: "lead-pencil"
icon_right_color: app.theme_cls.primary_color
hint_text: "Enter Word"
helper_text: "Write ONE word from the above sentence"
helper_text_mode: "on_focus"
multiline: False
pos_hint: {'center_x': 0.5, 'center_y': 0.6}
size_hint_x: None
width: root.width - dp(20)
MDIconButton:
icon: "brain"
pos_hint: {'center_x': 0.5, 'center_y': 0.4}
user_font_size: 64
on_press: app.root.get_screen('analyzer').analyze(root)
on_release: app.root.current = 'analyzer'
# MDRectangleFlatButton:
# text: "help"
# pos_hint: {'center_x': 0.75, 'center_y': .1}
# on_release: app.root.get_screen('main').show_data()
<Analyzer>:
BoxLayout:
orientation: 'vertical'
MDToolbar:
id: toolbar
title: "Probably Knot v3"
md_bg_color: app.theme_cls.accent_color
right_action_items: [["backburger", lambda x: main.change_screen(root)], ["help-circle-outline", lambda x: app.root.get_screen('main').show_data()]]
ScrollView:
MDList:
id: container
I have been having the same problem, I didn't find the perfect solution, but I found a way to remedy it.我一直有同样的问题,我没有找到完美的解决方案,但我找到了解决它的方法。
In place of the right_action_items
I used MDFloatActionButton
inside the MDToolbar
to look like this:我在
right_action_items
中使用MDFloatActionButton
了MDToolbar
,如下所示:
MDToolbar:
title: 'Settings'
elevation: 10
MDFloatingActionButton:
icon: 'keyboard-backspace'
theme_text_color: 'Custom'
elevation: 0
md_bg_color: app.theme_cls.primary_color
enter code here on_release: root.ids.screen_manager.current = 'Home'
Thanks Jacques answer.谢谢雅克的回答。 it works on my app.
它适用于我的应用程序。 I also found this solution in KivyMD https://github.com/kivymd/KivyMD/issues/213#issuecomment-598703010
我还在 KivyMD https://github.com/kivymd/KivyMD/issues/213#issuecomment-598703010中找到了这个解决方案
my code:我的代码:
# .kv file
MDToolbar:
type: "bottom"
right_action_items: [["clock", lambda x : app.set_screen('ss')]]
# 'ss' is the Screen name.
# .py file class Main(MDApp):
def set_screen(self, screen_name):
self.root.current = screen_name
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.