如何使用按钮更改全局变量

时间:2018-12-13 03:51:07

标签: python kivy

我正在开发一个程序,该程序应该是去我当地体育馆的人的助手。虽然我在使用全局变量difficult更改其字符串值时遇到麻烦。我希望能够按main_Screen中的任何按钮并更改difficult的字符串。因此,在BoulderingDirectory类中,它可以为举行练习的数据库执行相应的SQL。

注意:因为在体育馆中有多个部分,所以我已经截断了大部分代码,而我只是专注于此部分,因为它与其他部分完全相同。

screen_manager = ScreenManager()

global difficult

difficult = "Beginner"

class ScreenManagerApp(App):
   def build(self):
       return screen_manager

class MyScreenManager(ScreenManager):
   pass

class main_Screen(Screen):
   pass

class urec_facilities(Screen):
   pass

class AdventureDirectory(Screen):
   pass


class BoulderingDirectory(Screen):
    connection = sqlite3.connect("workoutData.db")
    cursor = connection.cursor()

    if (difficult == "Beginner"):
        cursor.execute("SELECT * FROM workoutData WHERE Activity = 
                       'Bouldering' and Difficulty = 'Beginner'")
    elif(difficult == "Intermediate"):
        cursor.execute("SELECT * FROM workoutData WHERE Activity = 
                        'Bouldering' and Difficulty = 'Intermediate'")
    else:
        cursor.execute("SELECT * FROM workoutData WHERE Activity = 
                       'Bouldering' and Difficulty = 'Advanced'")

    rows = StringProperty(str(cursor.fetchall()))


root_widget = Builder.load_string("""


<main_Screen>:
    canvas.before:
        Rectangle:
            pos: self.pos
            size: self.size
            source: "mainScreenImage.png"

Label:
    text: "Select your dificulty"
    font_size: 50
    color: (0.9,0.8,0,1)
    background_color: (0,0,0,0)
    pos_hint: {"x": 0, "y": 0.20}


Button:
    text: 'Beginner'
    font_size: 30
    size_hint: 0.2,0.2
    color: (0.9,0.8,0,1)
    background_color: (0,0,0,0)
    pos_hint: {"x": 0.16, "y": 0.1}
    on_release:
        #set the difficult variable to a string "Beginner"
        app.root.current = 'urecFac'



Button:
    text: 'Intermediate'
    font_size: 30
    size_hint: 0.2,0.2
    color: (0.9,0.8,0,1)
    background_color: (0,0,0,0)
    pos_hint: {"x": 0.41, "y": 0.1}
    on_release:
        #set the difficult variable to a string "Intermediate"
        app.root.current = 'urecFac'


Button:
    text: 'Advanced'
    font_size: 30
    size_hint: 0.2,0.2
    color: (0.9,0.8,0,1)
    background_color: (0,0,0,0)
    pos_hint: {"x": 0.66, "y": 0.1}
    on_release:
        #set the difficult variable to a string "Advanced"
        app.root.current = 'urecFac'

<urec_facilities>:
 BoxLayout:
    orientation: "vertical"
    Button:
        text: "Adventure Center"
        font_size: 50
        color: (0.9,0.8,0,1)
        background_normal: "adventureCenterImage.jpg"
        on_release: app.root.current = "AdventureDirectory"
    Button:
        background_normal: "mainScreenImage.png"
        text: "Back"
        font_size: 50
        color: (0.9,0.8,0,1)
        on_release: app.root.current = "mainScreen"

<AdventureDirectory>:
BoxLayout:
    orientation: "vertical"
    Button:
        text: "Bouldering"
        font_size: 50
        color: (0.9,0.8,0,1)
        on_release:
            app.root.current = "BoulderingDirectory"

    Button:
        text: "Go back"
        font_size: 50
        color: (0.9,0.8,0,1)
        on_release:
            app.root.current = "urecFac"

<BoulderingDirectory>:
BoxLayout:
    orientation: "vertical"
    Label:
        text: root.rows
        text_size: (root.width - 175), None
    Button:
        text: "Go back"
        font_size: 50
        color: (0.9,0.8,0,1)
        background_color: (0,0,0,1)
        on_release:
            app.root.current = "AdventureDirectory"

""")



screen_manager.add_widget(main_Screen(name = "mainScreen"))
screen_manager.add_widget(urec_facilities(name = "urecFac"))
screen_manager.add_widget(AdventureDirectory(name = "AdventureDirectory"))
screen_manager.add_widget(BoulderingDirectory(name = "BoulderingDirectory"))


ScreenManagerApp().run()

3 个答案:

答案 0 :(得分:1)

没有必要或不建议使用全局变量。不必要,因为更改全局变量的值不会通知副本更改,因此不建议这样做,因为它很难调试(有关更多信息,请阅读Why are global variables evil?)。

在这种情况下,解决方案是使用StringProperty,您可以将其连接到回调以更新数据。另一方面,必须在方法中实现请求的逻辑,以便可以多次调用它。考虑到上述因素,我已经对您的项目进行了重组,解决方案如下:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen
from kivy.properties import StringProperty
import sqlite3

class Main_Screen(Screen):
   difficult = StringProperty()

class Urec_facilities(Screen):
   pass

class AdventureDirectory(Screen):
   pass

class BoulderingDirectory(Screen):
    rows = StringProperty("")

    def load_from_difficult(self, difficult):
        connection = sqlite3.connect("workoutData.db")
        cursor = connection.cursor()
        cursor.execute("SELECT * FROM workoutData WHERE Activity = 'Bouldering' and Difficulty = ?", (difficult,))
        self.rows = str(cursor.fetchall())

root_widget = Builder.load_string("""
ScreenManager:
    Main_Screen:
        name: 'mainScreen'
        on_difficult: bouldering.load_from_difficult(self.difficult)
    Urec_facilities:
        name: 'urecFac'
    AdventureDirectory:
        name: 'AdventureDirectory'
    BoulderingDirectory:
        id: bouldering
        difficult: 'Beginner'
        name: 'BoulderingDirectory'

<Main_Screen>:
    canvas.before:
        Rectangle:
            pos: self.pos
            size: self.size
            source: "mainScreenImage.png"

    Label:
        text: "Select your dificulty"
        font_size: 50
        color: (0.9,0.8,0,1)
        background_color: (0,0,0,0)
        pos_hint: {"x": 0, "y": 0.20}

    Button:
        text: 'Beginner'
        font_size: 30
        size_hint: 0.2,0.2
        color: (0.9,0.8,0,1)
        background_color: (0,0,0,0)
        pos_hint: {"x": 0.16, "y": 0.1}
        on_release:
            root.difficult =  "Beginner"
            app.root.current = 'urecFac'

    Button:
        text: 'Intermediate'
        font_size: 30
        size_hint: 0.2,0.2
        color: (0.9,0.8,0,1)
        background_color: (0,0,0,0)
        pos_hint: {"x": 0.41, "y": 0.1}
        on_release:
            root.difficult = "Intermediate"
            app.root.current = 'urecFac'

    Button:
        text: 'Advanced'
        font_size: 30
        size_hint: 0.2,0.2
        color: (0.9,0.8,0,1)
        background_color: (0,0,0,0)
        pos_hint: {"x": 0.66, "y": 0.1}
        on_release:
            root.difficult = "Advanced"
            app.root.current = 'urecFac'

<Urec_facilities>:
    BoxLayout:
        orientation: "vertical"
        Button:
            text: "Adventure Center"
            font_size: 50
            color: (0.9,0.8,0,1)
            background_normal: "adventureCenterImage.jpg"
            on_release: app.root.current = "AdventureDirectory"
        Button:
            background_normal: "mainScreenImage.png"
            text: "Back"
            font_size: 50
            color: (0.9,0.8,0,1)
            on_release: app.root.current = "mainScreen"

<AdventureDirectory>:
    BoxLayout:
        orientation: "vertical"
        Button:
            text: "Bouldering"
            font_size: 50
            color: (0.9,0.8,0,1)
            on_release:
                app.root.current = "BoulderingDirectory"

        Button:
            text: "Go back"
            font_size: 50
            color: (0.9,0.8,0,1)
            on_release:
                app.root.current = "urecFac"

<BoulderingDirectory>:
    BoxLayout:
        orientation: "vertical"
        Label:
            text: root.rows
            text_size: (root.width - 175), None
        Button:
            text: "Go back"
            font_size: 50
            color: (0.9,0.8,0,1)
            background_color: (0,0,0,1)
            on_release:
                app.root.current = "AdventureDirectory"

""")

class ScreenManagerApp(App):
    def build(self):
        return root_widget

if __name__ == '__main__':
    ScreenManagerApp().run()

答案 1 :(得分:0)

在类或函数中使用全局变量之前,必须声明所引用的变量是全局变量,或者已将其创建为该定义的局部变量。

那么,

class BoulderingDirectory(Screen):
    global difficult
    ...

答案 2 :(得分:0)

通过global关键字,您可以在全局范围而不是局部范围内创建和修改变量。其语法为global variableName。因此,只需将global difficult添加到需要更改和/或访问全局变量的任何位置即可。

您可以将BoulderingDirectory类的开头更改为此:

class BoulderingDirectory(Screen):
    global difficult

有关global关键字的更多信息,请参见here