Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Kivy:单选按钮正确执行_Python_Python 3.x_Kivy - Fatal编程技术网

Python Kivy:单选按钮正确执行

Python Kivy:单选按钮正确执行,python,python-3.x,kivy,Python,Python 3.x,Kivy,我的应用程序有3个屏幕可以通过“ActionBar”导航。我在kv文件中命名了每个屏幕,一个是“跟踪”,另一个是“pess”。这是我需要帮助的两个 我在“pess”屏幕类中创建了一个radiobutton小部件(下图),以便添加电子邮件地址 在我的“追踪”屏幕课程中,我有所有我想通过电子邮件发送的数据,但我认为我把事情复杂化了。我想将激活的radiobutton从我的“pess”屏幕链接到我的“track”屏幕def发送功能 我的Main.py看起来像这样 class ScreenGenerat

我的应用程序有3个屏幕可以通过“ActionBar”导航。我在kv文件中命名了每个屏幕,一个是“跟踪”,另一个是“pess”。这是我需要帮助的两个

我在“pess”屏幕类中创建了一个radiobutton小部件(下图),以便添加电子邮件地址

在我的“追踪”屏幕课程中,我有所有我想通过电子邮件发送的数据,但我认为我把事情复杂化了。我想将激活的radiobutton从我的“pess”屏幕链接到我的“track”屏幕def发送功能

我的Main.py看起来像这样

class ScreenGenerator(ScreenManager):
    pass

class PessQuestions(Screen):
    email = {}
    path = ''

    def loadEmails(self, *args):
        self.path = App.get_running_app().user_data_dir + '/'
        try:
            with open(self.path + 'email.json', 'r') as data:
                self.email = json.load(data)
        except FileNotFoundError:
            pass

    def saveEmails(self, *args):
        print(self.ids.name.text, type(self.ids.name.text))
        print(self.ids.address.text, type(self.ids.address.text))
        self.email[self.ids.name.text] = self.ids.address.text

        self.path = App.get_running_app().user_data_dir + '/'
        with open(self.path + 'email.json', 'w') as email_address_json:
            json.dump(self.email, email_address_json)
            print('saveEmails')
        self.ids.address.text = ''
        self.ids.name.text = ''

        self.ids.info.clear_widgets()
        self.on_pre_enter() # refresh screen

    def delete_email(self, check):
        address = check.ids.who_name.text
        del self.email[address]

        self.ids.info.remove_widget(check)
        self.saveEmails()

##########################################################
########### PRE ENTER ####################################
    def on_pre_enter(self):
        self.ids.info.clear_widgets()
        self.loadEmails()
        try:
            for name, mail in self.email.items():
                self.ids.info.add_widget(Checkboxes(name=name, mail=mail, email=self.email))
        except ValueError:
            print('VALUE ERROR: Label.text accept only "str"')

        self.ids.pess_date.text = strftime('[b]%A[/b], %B %Y') # time and date

##########################################################
########### PRE LEAVE ####################################
    def on_pre_leave(self):
        self.ids.info.clear_widgets()
        self.saveEmails()

class Checkboxes(BoxLayout):
    def __init__(self, name='', mail='', email='', **kwargs):
        super().__init__(**kwargs)
        self.ids.who_name.text = name
        self.ids.who_email.text = mail

    def on_checkbox_Active(self, checkboxInstance, isActive):
        address = ''
        if isActive:
            '''
            Email is used for recipient
            '''
            name = self.ids.who_name.text
            email = self.ids.who_email.text
            print('Email Set!')
            return email
        else:
            '''
            Nothing happens, email is not used
            '''


##########################################################
######## TRACKERS WINDOWS ################################
##########################################################
class Trackers(Screen):
    email = {'davi': 'example@gmail.com'}
    storage = {}
    pess_storage = {}
    path = ''

    def on_pre_enter(self):
        self.path = App.get_running_app().user_data_dir + '/'
        self.loadData()
        for tracker, num in self.storage.items():
            self.ids.track.add_widget(Tracker(text=tracker, number=num))

    def on_pre_leave(self):
        self.ids.track.clear_widgets()
        self.saveData()

    def saveData(self, *args):
        with open(self.path + 'data.json', 'w') as data:
            json.dump(self.storage, data)

    def loadData(self, *args):
        try:
            with open(self.path + 'data.json', 'r') as data:
                self.storage = json.load(data)
                print(self.storage)
        except FileNotFoundError:
            pass

    def savePESS(self, PESS):
        self.pess_storage['physical'] = PESS.ids.phy_text.text
        self.pess_storage['emotional'] = PESS.ids.emo_text.text
        self.pess_storage['spiritual'] = PESS.ids.spi_text.text
        self.pess_storage['sexual'] = PESS.ids.sex_text.text
        self.pess_storage['comment'] = PESS.ids.comment.text

    def save_text(self, tracker, text_input, *args):
        tracker.ids.label.text = text_input.text
        self.storage[text_input.text] = '0'
        self.saveData()
        self.loadData()
        print('testing')

    def send(self, PESS):
        self.path = App.get_running_app().user_data_dir + '/'
        with open(self.path + 'bat', 'r') as bat:
            login = bat.read()
            davi = self.email['davi']
            mail = PESS.ids.who_email.text
            #kevin = self.email['kevin'] # instead of this I'd like to have this variable linked to 
            gmail = GMail(davi, login)

            day = strftime("%a, %b %d, %Y")
            tracker_message = 'PESS\n'
            subject_message = f"Subject: PESS {day}"

            for pess, txt in self.pess_storage.items():
                tracker_message += f"\t{pess.title()}: {txt}\n"

            for tracker, numbers in self.storage.items():
                tracker_message += f"\n{numbers} ---> {tracker}\n"

            message = f"{subject_message}\n\n{tracker_message}"
            msg = Message(f'PESS | {day}', to=mail, text=message)
            gmail.send(msg)

            self.sent_confirmation(f'{self.ids.who_name.text}: {mail}')

    def sent_confirmation(self, recipient):
        box = BoxLayout(orientation='vertical', padding=40, spacing=5)
        pop = Popup(title='Email Sent', content=box, size_hint=(None,None), size=(self.width, self.width/2))
        info = Label(text=f'Congrats, {recipient} has recieved an email.')
        box.add_widget(info)
        pop.open()
        self.saveData()


class Tracker(BoxLayout):
    def __init__(self, text='', number='', **kwargs):
        super().__init__(**kwargs)
        self.ids.label.text = text
        self.ids.count_add.text = number


class Pess(App):
    def build(self):
        Config.set('graphics', 'width', '600')
        Config.set('graphics', 'height', '800')
        from kivy.core.window import Window
        Window.clearcolor = get_color_from_hex('#262829')

        return ScreenGenerator()


if __name__ == '__main__':
    Pess().run()
kv文件

#: import rgba kivy.utils.get_color_from_hex
#: import CheckBox kivy.uix.checkbox
<Label>:
    font_size: '17dp'

<MenuButton@ButtonBehavior+Label>:
    canvas.before:
        Color:
            rgba: 0.1, 0.5, 0.7, 1
        Ellipse:
            pos: self.pos
            size: self.height, self.height
        Ellipse:
            pos: self.x + self.width - self.height, self.y
            size: self.height, self.height
        Rectangle:
            pos: self.x + self.height / 2, self.y
            size: self.width - self.height, self.height

<RoundButton@ButtonBehavior+Label>:
    canvas.before:
        Color:
            rgba: 0.8, 0.3, 0.1, 1
        Ellipse:
            pos: self.width / 2.265, self.y + 130
            size: self.height - self.height / 1.5, self.height / 3

<MenuButton2@ButtonBehavior+Label>:
    canvas.before:
        Color:
            rgba: 0.8, 0.3, 0.1, 1
        Ellipse:
            pos: self.pos
            size: self.height, self.height
        Ellipse:
            pos: self.x + self.width - self.height, self.y
            size: self.height, self.height
        Rectangle:
            pos: self.x + self.height / 2, self.y
            size: self.width - self.height, self.height

<FloatButton@ButtonBehavior+FloatLayout>:
    id: float_root
    size_hint: (None, None)
    text: '[b]+[/b]'
    font_size: '48dp'
    btn_size: (140,140)
    size: (140,140)
    bg_color: (0.8, 0.3, 0.1, 1)
    pos_hint: {'x': 5.4, 'y': .17}
    Button:
        text: float_root.text
        font_size: '14dp'
        #allow_stretch: True
        markup: True
        size_hint: (None, None)
        size: float_root.btn_size
        pos_hint: float_root.pos_hint
        background_normal: ''
        background_color: (0,1,0,0)
        canvas.before:
            Color:
                rgba: float_root.bg_color
            Ellipse:
                pos: self.pos
                size: self.size

<TrackerButton@Button>:
    background_color: 0,0,0,0


<ScreenGenerator>:
    Menu:
        name: 'menu'
    Trackers:
        name: 'track'
    PessQuestions:
        name: 'pess'

<Menu>:
    BoxLayout:
        orientation: 'vertical'
        padding: 20
        spacing: 30
        Image:
            source: 'book.png'
            allow_strech: True
        Label:
            canvas.before:
                Color: 
                    rgba: (1,1,1,.7)
                Rectangle:
                    size: self.size
                    pos: self.pos
            size_hint_y: None
            height: 1
        MenuButton:
            text: 'Enter Trackers'
            height: dp(60)
            size_hint_y: None
            background_image: ''
            background_color: rgba('#637075')
            on_release: app.root.current = 'track'
        MenuButton:
            text: 'Enter PESS'
            height: dp(60)
            size_hint_y: None
            background_image: ''
            background_color: rgba('#637075')
            on_release: app.root.current = 'pess'
        Label:
            canvas.before:
                Color: 
                    rgba: (1,1,1,.7)
                Rectangle:
                    size: self.size
                    pos: self.pos
            size_hint_y: None
            height: 1
        Label:
            id: time
            text: ''
            markup: True

<PessQuestions>:
    BoxLayout:
        orientation: 'vertical'
        pos_hint: {'top': 1}
        ActionBar:
            height: self.minimum_height + dp(50)
            size_hint_y: None
            background_image: ''
            background_color: rgba('#ffffff') # rgba('#0B3242')
            ActionView:
                ActionPrevious:
                    title: '[b]PESS[/b]'
                    font_size: '17dp'
                    color: rgba('#AFB7BA')
                    markup: True
                    on_release: app.root.current = 'track'
                ActionButton:
                    text: 'SEND'
                    color: rgba('#AFB7BA')
                    on_release: app.root.get_screen('track').send(root) # if .send() function is on another class outside of 'class PessQuestions' use the .get_screen() to locate the right screen class
                    #on_release: app.root.send()
                ActionButton:
                    text: 'TRACKER'
                    color: rgba('#AFB7BA')
                    on_release: app.root.current = 'track'
                ActionButton:
                    text: 'HOME'
                    color: rgba('#AFB7BA')
                    on_release: app.root.current = 'menu'

        BoxLayout:
            orientation: 'vertical'
            padding: dp(10)
            spacing: dp(12)
            pos_hint: {'top': 1}

            TextInput:
                id: phy_text
                hint_text: 'Physical'
                size_hint_y: None
                height: self.minimum_height + dp(7)
                multiline: False
            TextInput:
                id: emo_text
                hint_text: 'Emotional'
                size_hint_y: None
                height: self.minimum_height + dp(7)
                multiline: False
            TextInput:
                id: spi_text
                hint_text: 'Spiritual'
                size_hint_y: None
                height: self.minimum_height + dp(7)
                multiline: False
            TextInput:
                id: sex_text
                hint_text: 'Sexual'
                size_hint_y: None
                height: self.minimum_height + dp(7)
                multiline: False
            TextInput:
                id: comment
                hint_text: 'Leave a comment'
                size_hint_y: None
                height: self.minimum_height + dp(100)

            MenuButton:
                id: save_pess
                text: 'Save PESS'
                height: dp(60)
                size_hint_y: None
                background_image: ''
                on_press: self.text = 'SAVED!'
                on_release: app.root.get_screen('track').savePESS(root)

            MenuButton2:
                text: 'Clear PESS'
                height: dp(60)
                size_hint_y: None
                background_image: ''
                on_press: root.ids.save_pess.text = 'Save PESS'
                on_release: root.resetPESS()

            MenuButton:
                text: 'Send Report'
                color: rgba('#AFB7BA')
                height: dp(60)
                size_hint_y: None
                background_image: ''
                on_release: app.root.get_screen('track').send(root) # if .send() function is on another class outside of 'class PessQuestions' use the .get_screen() to locate the right screen class
                #on_release: app.root.send()

            Label:
                canvas.before:
                    Color: 
                        rgba: (1,1,1,.7)
                    Rectangle:
                        size: self.size
                        pos: self.pos
                size_hint_y: None
                height: 1

            Label:
                text: '[b]Physical, Emotional, Spiritual, Sexual[/b] | These principles when lived with a full purpose of heart will invite the Holy Ghost into your lives.'
                markup: True
                text_size: self.width, None
                size_hint: 1, None
                height: self.texture_size[1]
            Label:
                id: pess_date
                text: ''
                markup: True

            Label:
                canvas.before:
                    Color: 
                        rgba: (1,1,1,.7)
                    Rectangle:
                        size: self.size
                        pos: self.pos
                size_hint_y: None
                height: 1

            # Label & Checkbox creation | for selecting email address
            Label:
                text: "EMAIL"
                text_size: self.width, None
                size_hint: 1, None
                height: self.texture_size[1]
            ScrollView:
                BoxLayout:
                    id: info
                    orientation: 'vertical'
                    padding: dp(5)
                    spacing: dp(7)
                    #size_hint_y: None
                    #height: self.minimum_height + dp(50)
            TextInput:
                id: name
                hint_text: 'Name'
                size_hint_y: None
                height: self.minimum_height + dp(7)
                multiline: False
            TextInput:
                id: address
                hint_text: 'Email Address'
                size_hint_y: None
                height: self.minimum_height + dp(7)
                multiline: False
            MenuButton:
                id: save_email
                text: 'Save Email'
                height: dp(60)
                size_hint_y: None
                background_image: ''
                on_release: root.saveEmails()


<Checkboxes>:
    CheckBox:
        group: 'emails'
        color: .294, .761, .623
        on_active: root.on_checkbox_Active(self, self.active)
        #on_active: app.root.get_screen('pess').on_checkbox_Active(self, self.active)
        size_hint_x: .50
    Label:
        id: who_name
        text: ''
        text_size: self.width, None
        halign: 'left'
    Label:
        id: who_email
        text: ''
        text_size: self.width, None
        halign: 'left'
    Button:
        text: '[b]X[/b]'
        markup: True
        size_hint_x: None
        width: dp(20)
        on_release: app.root.get_screen('pess').delete_email(root)

<Trackers>:
    BoxLayout:
        orientation: 'vertical'
        ActionBar:
            height: self.minimum_height + dp(50)
            size_hint_y: None
            background_image: ''
            background_color: rgba('#ffffff') #rgba('#0B3242')
            ActionView:
                ActionPrevious:
                    title: '[b]TRACKERS[/b]'
                    font_size: '17dp'
                    color: rgba('#AFB7BA')
                    markup: True
                    on_release: app.root.current = 'pess'
                ActionButton:
                    text: 'RESET'
                    color: rgba('#AFB7BA')
                    on_release: root.reset_pop()
                ActionButton:
                    text: 'PESS'
                    color: rgba('#AFB7BA')
                    on_release: app.root.current = 'pess'
                ActionButton:
                    text: 'HOME'
                    color: rgba('#AFB7BA')
                    on_release: app.root.current = 'menu'
        ScrollView:
            BoxLayout:
                id: track
                orientation: 'vertical'
                padding: dp(10)
                spacing: dp(15)
                size_hint_y: None
                height: self.minimum_height
        BoxLayout:
            size_hint_y: None
            height: dp(100)
            Button:
                text: '+'
                font_size: '56dp'
                size_hint_y: None
                background_image: ''
                background_color: (0,0,0,0)
                on_release: root.addWidget()

        #BoxLayout:
            #FloatButton:
            #    pos_hint_x: None
            #    pos_hint_y: None
            #    on_release: root.addWidget()

<Tracker>:
    count_add: count_add
    name: name
    size_hint_y: None
    height: 130

    canvas.before:
        Color:
            rgba: 0.1, 0.5, 0.7, 1
        Rectangle:
            pos: self.pos[0] + self.height/2, self.pos[1]
            size: self.size[0] - self.height, self.height
        Ellipse:
            pos: self.pos[0], self.pos[1]
            size: self.height, self.height
        Ellipse:
            pos: self.pos[0] + self.width - self.height, self.pos[1]
            size: self.height, self.height

    TrackerButton:
        text: '[b]X[/b]'
        markup: True
        size_hint_x: None
        width: 120
        on_release: app.root.get_screen('track').delete_storage(root)

    Label:
        id: name
        canvas.before:
            Color: 
                rgba: (1,1,1,.7)
            Rectangle:
                size: self.size
                pos: self.pos
        size_hint_x: None
        width: 1

    TrackerButton:
        id: label
        font_size: '16dp'
        on_release: app.root.get_screen('track').change_name(root)

    TrackerButton:
        id: count_add
        font_size: '16dp'
        text: '0'
        size_hint_x: None
        width: 120
        on_release: app.root.get_screen('track').add_num(root)

    Label:
        canvas.before:
            Color: 
                rgba: (1,1,1,.7)
            Rectangle:
                size: self.size
                pos: self.pos
        size_hint_x: None
        width: 1

    TrackerButton:
        text: '[b]-[/b]'
        font_size: '24dp'
        markup: True
        size_hint_x: None
        width: 120
        on_release: app.root.get_screen('track').subtract_num(root)
:导入rgba kivy.utils.get_color_from_hex
#:导入复选框kivy.uix.CheckBox
:
字体大小:“17dp”
:
在以下情况之前:
颜色:
rgba:0.1,0.5,0.7,1
椭圆:
pos:self.pos
尺寸:自我高度,自我高度
椭圆:
位置:自x+自宽-自高,自y
尺寸:自我高度,自我高度
矩形:
位置:自我x+自我高度/2,自我y
大小:self.width-self.height,self.height
:
在以下情况之前:
颜色:
rgba:0.8,0.3,0.1,1
椭圆:
位置:自宽/2.265,自y+130
尺寸:self.height-self.height/1.5,self.height/3
:
在以下情况之前:
颜色:
rgba:0.8,0.3,0.1,1
椭圆:
pos:self.pos
尺寸:自我高度,自我高度
椭圆:
位置:自x+自宽-自高,自y
尺寸:自我高度,自我高度
矩形:
位置:自我x+自我高度/2,自我y
大小:self.width-self.height,self.height
:
id:float_root
大小提示:(无,无)
正文:'[b]+[/b]'
字体大小:“48dp”
btn_尺寸:(140140)
尺寸:(140140)
背景颜色:(0.8,0.3,0.1,1)
位置提示:{'x':5.4,'y':.17}
按钮:
text:float_root.text
字体大小:“14dp”
#允许拉伸:真
标记:正确
大小提示:(无,无)
大小:float\u root.btn\u大小
pos\u提示:float\u root.pos\u提示
背景\u正常:“”
背景颜色:(0,1,0,0)
在以下情况之前:
颜色:
rgba:float\u root.bg\u颜色
椭圆:
pos:self.pos
大小:self.size
:
背景颜色:0,0,0,0
:
菜单:
名称:“菜单”
追踪器:
名称:'轨道'
问题:
名称:'pess'
:
盒子布局:
方向:“垂直”
填充:20
间距:30
图片:
资料来源:“book.png”
允许:对
标签:
在以下情况之前:
颜色:
rgba:(1,1,1,7)
矩形:
大小:self.size
pos:self.pos
尺寸提示:无
身高:1
菜单按钮:
文本:“输入跟踪器”
高度:dp(60)
尺寸提示:无
背景图像:“”
背景颜色:rgba(“#637075”)
发布时:app.root.current='track'
菜单按钮:
文本:“输入PESS”
高度:dp(60)
尺寸提示:无
背景图像:“”
背景颜色:rgba(“#637075”)
发布时:app.root.current='pess'
标签:
在以下情况之前:
颜色:
rgba:(1,1,1,7)
矩形:
大小:self.size
pos:self.pos
尺寸提示:无
身高:1
标签:
id:时间
文本:“”
标记:正确
:
盒子布局:
方向:“垂直”
位置提示:{'top':1}
操作栏:
高度:自身最小高度+dp(50)
尺寸提示:无
背景图像:“”
背景颜色:rgba(“#ffffff”)#rgba(“#0B3242”)
ActionView:
以前的行动:
标题:“[b]比索[/b]”
字体大小:“17dp”
颜色:rgba(“#AFB7BA”)
标记:正确
发布时:app.root.current='track'
操作按钮:
文本:“发送”
颜色:rgba(“#AFB7BA”)
发布时:app.root.get_screen('track').send(root)#如果.send()函数位于'class PessQuestions'之外的另一个类上,请使用.get_screen()找到正确的screen类
#发布时:app.root.send()
操作按钮:
文本:“跟踪器”
颜色:rgba(“#AFB7BA”)
发布时:app.root.current='track'
操作按钮:
文字:“家”
颜色:rgba(“#AFB7BA”)
发布时:app.root.current='菜单'
盒子布局:
方向:“垂直”
填充:dp(10)
间距:dp(12)
位置提示:{'top':1}
文本输入:
id:phy_text
提示文字:“物理”
尺寸提示:无
高度:自身最小高度+dp(7)
多行:False
文本输入:
id:emo_文本
提示文字:“情绪化”
尺寸提示:无
高度:自身最小高度+dp(7)
多行:False
文本输入:
id:spi_文本
提示文字:“精神的”
尺寸提示:无
高度:自身最小高度+dp(7)
多行:False
文本输入:
id:sex_文本
提示文字:“性”
尺寸提示:无
高度:自身最小高度+dp(7)
多行:False
文本输入:
class MainInterface(BoxLayout):
    def __init__(self, **kwargs):
        super(MainInterface, self).__init__(**kwargs)

class WindowManager(ScreenManager):
    pass

class HomeWindow(Screen):
    ...
    
class TrackerWindow(Screen):
    ...
    
sm = WindowManager()
screens = [HomeWindow(name="Home"), TrackerWindow(name="TrackerWindow")]
for screen in screens:
    sm.add_widget(screen)
    
class MyMainApp(App):
    def build(self):
        root = MainInterface()
        return root

<MainInterface>:
    WindowManager:
        id: screenmanager
        HomeWindow:
            id: homewindow
            ...
            Button:
                pos_hint:{"x":0,"top":1}
                size_hint: 0.125, 0.1
                text: "Some Stuff"
                font_size: (root.width**1 + root.height**1) / 10**2
                on_release:
                    root.ids['trackerwindow'].send(YourData)
                    root.ids['homewindow'].access_user_inputs(self.parent.user_inputs)
                    root.ids['screenmanager'].transition.direction = "left"
                    root.ids['screenmanager'].current="TrackerWindow"
        TrackerWindow:
            id: trackerwindow
            ...