Python Kivy MDToolBar右(行动)项:Can';不要换屏幕
就我的一生而言,我似乎不知道如何使用MDToolBarPython Kivy MDToolBar右(行动)项:Can';不要换屏幕,python,python-3.x,kivy,toolbar,kivy-language,Python,Python 3.x,Kivy,Toolbar,Kivy Language,就我的一生而言,我似乎不知道如何使用MDToolBarright\u action\u items:来更改屏幕。我知道,通过发布上的按钮:我可以添加一个简单的app.root.current='analyzer'(我将我的屏幕定义为'analyzer'和'main') 但是,您不能将此代码添加到右侧\u操作\u项:Lambda x:app.root.current='analyzer',单位为kv。 所以我尝试在python中添加一个函数,在我的管理器(ScreenManager)--> 但它不
right\u action\u items:
来更改屏幕。我知道,通过发布上的按钮:
我可以添加一个简单的app.root.current='analyzer'
(我将我的屏幕定义为'analyzer'和'main')
但是,您不能将此代码添加到右侧\u操作\u项:Lambda x:app.root.current='analyzer'
,单位为kv。
所以我尝试在python中添加一个函数,在我的管理器(ScreenManager)
-->
但它不起作用。。。我迷路了,困惑,害怕。。。救命啊。我请
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()
千伏
:
主要内容:
id:main
名称:'main'
分析仪:
id:分析
名称:'分析器'
:
盒子布局:
方向:“垂直”
MDToolbar:
id:工具栏
标题:“可能结v3”
md_bg_颜色:app.theme_cls.primary_颜色
右\操作\项:[[“帮助圆轮廓”,lambda x:app.root.get\屏幕('main')。显示\数据()]
MDF布局:
MDTextField:
id:句子
图标右:“书本打开大纲”
图标\u右\u颜色:app.theme\u cls.primary\u颜色
提示文字:“输入句子”
helper_text:“编写问题陈述以进行分析”
帮助器\u文本\u模式:“打开焦点”
多行:False
位置提示:{'center_x':0.5,'center_y':0.7}
大小提示:无
宽度:根部宽度-dp(20)
MDTextField:
id:word
图标右:“铅笔”
图标\u右\u颜色:app.theme\u cls.primary\u颜色
提示文字:“输入单词”
helper_text:“从上述句子中写出一个单词”
帮助器\u文本\u模式:“打开焦点”
多行:False
位置提示:{'center_x':0.5,'center_y':0.6}
大小提示:无
宽度:根部宽度-dp(20)
MDIconButton:
图标:“大脑”
位置提示:{'center_x':0.5,'center_y':0.4}
用户\字体\大小:64
按:app.root.get_屏幕('analyzer')。分析(root)
发布时:app.root.current='analyzer'
#MDRectangleFlatButton:
#文本:“帮助”
#位置提示:{'center_x':0.75,'center_y':.1}
#发布时:app.root.get_screen('main')。show_data()
:
盒子布局:
方向:“垂直”
MDToolbar:
id:工具栏
标题:“可能结v3”
md_bg_颜色:app.theme_cls.accent_颜色
右行动项目:[[“backburger”,lambda x:main.change\u screen(root)],[“help circle outline”,lambda x:app.root.get\u screen('main')。show\u data()]
滚动视图:
MDList:
id:集装箱
我也遇到了同样的问题,我没有找到完美的解决方案,但我找到了补救办法
我在MDToolbar
中使用MDFloatActionButton
来代替right\u action\u项,如下所示:
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'
谢谢,很高兴知道我没有疯!!我试试看:)瓦鲁!成功了!:)现在唯一的问题是,按下图标后,退格会给我一个奇怪的黑色圆圈轮廓。您遇到过这种情况吗?实际上,我刚刚发现我的MDToolBar
中的MDIconButton
似乎工作得更好。无圆阴影(我认为它来自于MDFloatingActionButton
中的elevation:
属性。再次感谢您,您帮助我看到了框外:)
<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
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'