我正在尝试重新组织我的代码,因此它不是全部都放在一个py和kv文件中。我为五星级审查功能创建了一些逻辑,当我将所有代码放入Reviews(Screen)
类并用on_press: root.method()
调用方法时,该逻辑工作正常。但是,当代码不在同一类中时,我不知道用root.method()
替换什么。
main.py文件:
import kivy
from kivymd.app import MDApp
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from reviews import Reviews
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.core.window import Window
class ReviewWindow(Screen):
pass
class WindowManager(ScreenManager):
pass
class MyApp(MDApp):
def build(self):
kv = Builder.load_file("kivy.kv")
self.sm = WindowManager()
screens = [ReviewWindow(name="reviews")]
for screen in screens:
self.sm.add_widget(screen)
self.sm.current = "reviews"
return self.sm
if __name__ == '__main__':
MyApp().run()
kivy.kv文件:
<ReviewWindow>:
name: "reviews"
GridLayout:
cols: 1
GridLayout:
GridLayout:
orientation: "horizontal"
cols: 7
Label:
MDIconButton:
icon: "star-outline"
id: star_1
on_press: root.star_1_pressed()
MDIconButton:
icon: "star-outline"
id: star_2
on_press: root.star_2_pressed()
MDIconButton:
icon: "star-outline"
id: star_3
on_press: root.star_3_pressed()
MDIconButton:
icon: "star-outline"
id: star_4
on_press: root.star_4_pressed()
MDIconButton:
icon: "star-outline"
id: star_5
on_press: root.star_5_pressed()
Label:
GridLayout:
cols: 3
Label:
MDRaisedButton:
text: "review"
on_release: root.submit_review()
Label:
GridLayout:
reviews.py文件:
from kivymd.uix.button import MDIconButton
from kivymd.toast import toast
class Reviews():
review_number = None
def star_1_pressed(self):
self.review_number = 1
self.clear_stars()
self.ids.star_1.icon = "star"
self.ids.star_1.theme_text_color = "Custom"
self.ids.star_1.text_color = [1,0.843,0,1]
def star_2_pressed(self):
self.review_number = 2
self.clear_stars()
self.ids.star_1.icon = "star"
self.ids.star_2.icon = "star"
self.ids.star_1.theme_text_color = "Custom"
self.ids.star_1.text_color = [1,0.843,0,1]
self.ids.star_2.theme_text_color = "Custom"
self.ids.star_2.text_color = [1,0.843,0,1]
def star_3_pressed(self):
self.review_number = 3
self.clear_stars()
self.ids.star_1.icon = "star"
self.ids.star_2.icon = "star"
self.ids.star_3.icon = "star"
self.ids.star_1.theme_text_color = "Custom"
self.ids.star_1.text_color = [1,0.843,0,1]
self.ids.star_2.theme_text_color = "Custom"
self.ids.star_2.text_color = [1,0.843,0,1]
self.ids.star_3.theme_text_color = "Custom"
self.ids.star_3.text_color = [1,0.843,0,1]
def star_4_pressed(self):
self.review_number = 4
self.clear_stars()
self.ids.star_1.icon = "star"
self.ids.star_2.icon = "star"
self.ids.star_3.icon = "star"
self.ids.star_4.icon = "star"
self.ids.star_1.theme_text_color = "Custom"
self.ids.star_1.text_color = [1,0.843,0,1]
self.ids.star_2.theme_text_color = "Custom"
self.ids.star_2.text_color = [1,0.843,0,1]
self.ids.star_3.theme_text_color = "Custom"
self.ids.star_3.text_color = [1,0.843,0,1]
self.ids.star_4.theme_text_color = "Custom"
self.ids.star_4.text_color = [1,0.843,0,1]
def star_5_pressed(self):
self.review_number = 5
self.clear_stars()
self.ids.star_1.icon = "star"
self.ids.star_2.icon = "star"
self.ids.star_3.icon = "star"
self.ids.star_4.icon = "star"
self.ids.star_5.icon = "star"
self.ids.star_1.theme_text_color = "Custom"
self.ids.star_1.text_color = [1,0.843,0,1]
self.ids.star_2.theme_text_color = "Custom"
self.ids.star_2.text_color = [1,0.843,0,1]
self.ids.star_3.theme_text_color = "Custom"
self.ids.star_3.text_color = [1,0.843,0,1]
self.ids.star_4.theme_text_color = "Custom"
self.ids.star_4.text_color = [1,0.843,0,1]
self.ids.star_5.theme_text_color = "Custom"
self.ids.star_5.text_color = [1,0.843,0,1]
def clear_stars(self):
self.ids.star_1.theme_text_color = "Custom"
self.ids.star_1.text_color = [0,0,0,1]
self.ids.star_2.theme_text_color = "Custom"
self.ids.star_2.text_color = [0,0,0,1]
self.ids.star_3.theme_text_color = "Custom"
self.ids.star_3.text_color = [0,0,0,1]
self.ids.star_4.theme_text_color = "Custom"
self.ids.star_4.text_color = [0,0,0,1]
self.ids.star_5.theme_text_color = "Custom"
self.ids.star_5.text_color = [0,0,0,1]
def submit_review(self):
if self.review_number:
toast("{} star review given".format(self.review_number))
else:
toast("Please click a star")
在您的reviews.py
更改中:
class Reviews():
to
class Reviews(Screen):
然后在kivy.kv
更改中:
<ReviewWindow>:
to
<Reviews>:
最后,在您的main.py
中,您可以消除ReviewWindow
类:
import kivy
from kivymd.app import MDApp
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from reviews import Reviews
class WindowManager(ScreenManager):
pass
class MyApp(MDApp):
def build(self):
kv = Builder.load_file("kivy.kv")
self.sm = WindowManager()
screens = [Reviews(name="reviews")]
for screen in screens:
self.sm.add_widget(screen)
self.sm.current = "reviews"
return self.sm
if __name__ == '__main__':
MyApp().run()