首页 > 解决方案 > kivyMD:将数据写入文件并评估测试

问题描述

我是初学者,我正在尝试在 kivymd 中进行性格测试。有 24 个问题,您可以从 4 个选项(d、i、s、n)中选择一个。

在评估测试时,我想写出用户选择了多少特定字母。我想把它保存到一个文件中。我已经从文本字段中写了一个测试名称,但我希望它作为元组或列表。

我认为是这样的:

 Test1[d=2,i=5,s=8,n=9]

你知道我该怎么做吗?我尝试使用函数 d(),.. 但它不起作用

主文件

class ItemConfirm(OneLineAvatarIconListItem):
    divider = None

    def set_icon(self, choice):
        choice.active = True
        check_list = choice.get_widgets(choice.group)
        for check in check_list:
            if check != choice:
                check.active = False

class HomeScreen(Screen):
    choose_dialog= None
    choice = None
    help_dialog= None

    def show_ChooseDialog(self):
        self.choose_dialog = MDDialog(title="Aim:", type="confirmation",
                size_hint=[0.9, 0.5], auto_dismiss=False,
                items=[ItemConfirm(text="personaly",on_release= self.next_page_me,
                                   on_press= self.close_choose_dialog,)],)
        self.choose_dialog.open()


    def close_choose_dialog(self, obj):
        self.choose_dialog.dismiss()
        print(self.choose_dialog.items)

    def next_page_me (self,obj):
        self.manager.current = "motivationme"

    def show_HelpDialog(self):
        ok_button = MDRectangleFlatButton (text= "ok",on_release=self.close_help_dialog)
        self.help_dialog = MDDialog(title="info about test", text="bla bla",
                              size_hint=[0.8, 0.5], auto_dismiss=False,
                              buttons=[ok_button])
        self.help_dialog.open()

    def close_help_dialog(self,obj):
        self.help_dialog.dismiss()

class MotivationScreenMe(Screen):

    def add_test(self):
        file = open("package.txt", "w")
        file.write(self.ids.nazov_testu.text)

class TestConfirm(OneLineAvatarIconListItem):
    divider = None

    def set_icon(self, choice):
        choice.active = True
        check_list = choice.get_widgets(choice.group)
        for check in check_list:
            if check != choice:
                check.active = False

class TestScreenV(Screen):
    d= 0
    i= 0
    s=0
    n=0
    snackbar = None
    p = 0
    r = 0
    def d(self,d):
        self.d= d+1
        print(d)
    def i(self,i):
        self.i=i+1
        print(i)
    def s(self,s):
        self.s=s+1
        print(s)
    def n(self,n):
        self.n=n+1
        print(n)
    def show_example_snackbar(self):
        self.snackbar = Snackbar(text="finish?",
                                    snackbar_x="10dp",
                                    snackbar_y="10dp",
                                 bg_color= (0.96,0.79,0.09, 1),)
        self.snackbar.size_hint_x = ( Window.width -
                                      (self.snackbar.snackbar_x * 2)) / Window.width
        self.snackbar.buttons = [MDFlatButton(text="yes",text_color=(1, 1, 1, 1),
                                              on_release=self.evaluate,
                                              on_press= self.snackbar.dismiss),]
        self.snackbar.open()

    def evaluate (self,obj):
        self.manager.current= "testm"
        self.manager.transition.direction = 'left'

    def plus(self):
        self.p = self.p +4.16
        self.ids.progress.value = self.p

class MainApp(MDApp):

    def build(self):
        self.theme_cls.primary_palette = "Red"
        self.theme_cls.primary_hue = "500"
        self.theme_cls.theme_style = "Light"


        screen = Builder.load_string(screen_helper)
        return screen

MainApp().run()


.kv

screen_helper = """
ScreenManager:
    HomeScreen:
    MotivationScreenMe:
    TestScreenV:
    
  
<HomeScreen>:
    name: "home"

    Image:
        source: "all.png"
        pos_hint: { "center_x" :0.5, "center_y":0.5}
        size_hint: (1,1)

    MDFloatingActionButton:
        icon: "play-circle-outline"
        size_hint: None, None
        pos_hint: {"center_x" :0.5, "center_y":0.08}
        md_bg_color: app.theme_cls.primary_color
        on_release: root.show_ChooseDialog()

    MDFloatingActionButton:
        icon: "help-circle-outline"
        md_bg_color: app.theme_cls.primary_color
        pos_hint: { "center_x" :0.15, "center_y":0.08}
        on_release: root.show_HelpDialog()


<ItemConfirm>
    on_release: root.set_icon(check)

    CheckboxLeftWidget:
        id: check
        group: "check"            

<MotivationScreenMe>:
    name: "motivationme"
    nazov_testu: nazov_testu

    MDCard:
        orientation: "vertical"
        pos_hint:{ "center_x" :0.5, "center_y": 0.6} 
        size_hint: 0.8, 0.7
        padding: "8dp"

        MDLabel:
            text: "why.."
            font_size: (root.width**2 + root.height**2) / 13**4
            halign: "center"
            pos_hint: { "center_x" :0.3, "center_y":0.95}
            size_hint: 0.8, 0.1
            theme_text_color: "Custom"
            text_color: app.theme_cls.primary_color

        MDSeparator:
            height: "1dp"

        MDLabel:
            text: "bla bla bla bla bla "
            font_size: (root.width**2 + root.height**2) / 13**4
            halign: "center"

    MDTextField:
        id: nazov_testu
        hint_text:"nazov_testu(it means) = test_name"
        helper_text: "eg. Test 1"
        helper_text_mode: "on_focus"
        pos_hint: { "center_x" :0.5, "center_y":0.2}
        size_hint: 0.8,0.1 

    MDFloatingActionButton:
        icon: "play-circle-outline"
        pos_hint: { "center_x" :0.5, "center_y":0.08}
        md_bg_color: app.theme_cls.primary_color
        on_release: 
            root.manager.current = "testv"
            root.manager.transition.direction = 'up'
        on_press: root.add_test()

<TestConfirm>
    on_release: root.set_icon(check)

    CheckboxLeftWidget:
        id: check 

<TestScreenV>:
    name: "testv"
    MDCard:
        orientation: "vertical"
        padding: "8dp"
        size_hint: 0.9,0.7
        pos_hint: {"center_x": .5, "center_y": .6}

        OneLineListItem:                          
            text: "chose option which fits on you the best"
            theme_text_color: "Custom"
            text_color: app.theme_cls.primary_color
            font_size: (root.width**2 + root.height**2) / 13**4

        ScrollView:
            do_scroll_x: False
            BoxLayout:
                orientation: 'vertical'
                size_hint_y: None
                height: dp(350)

                OneLineListItem:
                    text: "1/24 "
                    theme_text_color: "Custom"
                    text_color: app.theme_cls.primary_color
                    
                TestConfirm:
                    text: "option which means point s"
                    font_size: (root.width**2 + root.height**2) / 13**4
                    on_press: 
                        root.plus()
                        root.s()
                TestConfirm:
                    text: "option which means point n"
                    font_size: (root.width**2 + root.height**2) / 13**4
                    on_press: 
                        root.plus()
                        root.n()
                TestConfirm:    
                    text: "option which means point d"
                    font_size: (root.width**2 + root.height**2) / 13**4
                    on_press: 
                        root.plus()
                        root.d()
                TestConfirm:
                    text: "option i"
                    font_size: (root.width**2 + root.height**2) / 13**4
                    on_press: 
                        root.plus()
                        root.i()

 MDFloatingActionButton:
        icon: "check-circle-outline"
        pos_hint: { "center_x" :0.5, "center_y":0.08}
        md_bg_color: app.theme_cls.primary_color
        on_release: root.show_example_snackbar()


    MDProgressBar:
        id: progress
        size_hint_x: 0.8
        size_hint_y: 0.08
        color: app.theme_cls.accent_color
        pos_hint: { "center_x" :0.5, "center_y":0.2}



标签: pythonkivykivymd

解决方案


推荐阅读