Skip to main content
 首页 » 编程设计

python之如何使用 Button 更改全局变量

2024年12月31日9fff_TT

我正在开发一个程序,该程序应该是去我本地健身房的人的助手。尽管我在使用全局变量 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() 

请您参考如下方法:

没有必要也不建议使用全局变量。没有必要,因为更改全局变量的值不会通知副本的更改,并且不建议这样做,因为它很难调试(有关更多信息,请阅读 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()