在没有类的情况下,变量(Pyhton / Pygame)的范围有问题

问题描述 投票:0回答:1

我在我的

globals.py
上创建了一个变量,在函数名称生成播放列表上对她进行了一些修改,并尝试在另一个函数上使用她。

  1. 全局.py
import pygame as Py

selectedSong = None
  1. 函数generatePlaylist并在playlist.py上进行选择
from globals import *
import os

songs = os.listdir('./assets/songs')  

def generatePlaylist(font, event):
    for index, song in enumerate(songs):
        rectIndex = Py.Rect(20, 25 + (50 * (index + 1)), 260, 40)
        rectIndexPosition = (20, 25 + (50 * (index + 1)))
        rectIndexWidth = 260
        rectIndexHeight = 40
        Py.draw.rect(screen, 'gray', rectIndex)
        text_surface = font.render(song, True, (0, 0, 0))
        text_rect = text_surface.get_rect(center=rectIndex.center)
        screen.blit(text_surface, text_rect)

        selected = selection(event, rectIndexPosition, rectIndexWidth, rectIndexHeight, song)
        if selected is not None:
            selectedSong = selected
            print(selectedSong)

        if index == len(songs) - 1:
            text_surface = font.render(song, True, (0, 0, 0))
            text_rect = text_surface.get_rect(center=rectIndex.center)
            screen.blit(text_surface, text_rect)
            rectDownload = Py.Rect(20, 25 + (50 * (index + 2)), 260, 40)
            Py.draw.rect(screen, 'gray', rectDownload)
            text_surface = font.render("Download", True, (0, 0, 0))
            text_rect = text_surface.get_rect(center=rectDownload.center)
            screen.blit(text_surface, text_rect)

def selection(event, rectIndexPosition, rectIndexWidth, rectIndexHeight, song):
    if event.type == Py.MOUSEBUTTONUP:
            if rectIndexPosition[0] <= event.pos[0] <= rectIndexPosition[0] + rectIndexWidth and \
               rectIndexPosition[1] <= event.pos[1] <= rectIndexPosition[1] + rectIndexHeight:
                return(song)
    return None    

我在这里打印说当我点击时selectedSong已经更新了

  1. 我的main.py
import pygame as Py
from render import *
from buttonMusic import *
from playlist import *
from globals import *

Py.init()

continuer = True
script_folder = os.path.dirname(os.path.abspath("C:\\Users\\Anthony\\OneDrive\\Bureau\\playerMusic\\assets"))
font_path = os.path.join(script_folder, 'assets', 'font', 'Roboto-Black.ttf')
font = Py.font.Font(font_path, 18)

while continuer:
    render(font)
    for event in Py.event.get():
        if event.type == Py.QUIT:
            continuer = False
        generatePlaylist(font, event)
        reculeButton(event)
        randomButton(event)
        playButton(event)
        pauseButton(event)
        stopButton(event)
        advanceButton(event)
        loopButton(event)
        upButton(event)
        downButton(event)
        muteButton(event)
Py.quit()

在generatePlaylist之后调用playButton

  1. buttonMusic.py 上的播放按钮
from musicFunction import *
from globals import *

def playButton(event):
   if event.type == Py.MOUSEBUTTONDOWN:
      if imagePlayPosition[0] <= event.pos[0] <= imagePlayPosition[0] + imagePlay.get_width() and \
         imagePlayPosition[1] <= event.pos[1] <= imagePlayPosition[1] + imagePlay.get_height():
         print(selectedSong)
         if selectedSong is not None:
            play()

当我点击时,我的打印一次又一次停留在“无”上 5) 不确定你需要它,但我的函数在 musicFunction.py 上播放

def play():
    mx.music.load(f'./assets/songs/{selectedSong}')
    mx.music.play()

如果您需要它,我可以给您我的 github 存储库,感谢您花时间解决我的问题。

哦,一个新的空间来解释我的问题,我尝试了很多东西。很难解释什么是将我的变量从全局变量更改为函数参数等等。我所期望的只是找到一个可以向我解释为什么它不起作用的人。

python scope
1个回答
0
投票

你做错的是这一行:

from globals import *

这告诉 python 为每个模块提供自己的副本,在本例中:

selectedSong = None

每个模块然后继续修改自己的变量。

您应该阅读 Ned Batchelder 的这篇博客文章

作为快速解决方案,我可以建议:

  1. 全局.py
selectedSong = None

然后,作为所需其他更改的示例:

  1. 函数generatePlaylist并在playlist.py上进行选择
import globals
import os

songs = os.listdir('./assets/songs')  

def generatePlaylist(font, event):
    for index, song in enumerate(songs):
        rectIndex = Py.Rect(20, 25 + (50 * (index + 1)), 260, 40)
        rectIndexPosition = (20, 25 + (50 * (index + 1)))
        rectIndexWidth = 260
        rectIndexHeight = 40
        Py.draw.rect(screen, 'gray', rectIndex)
        text_surface = font.render(song, True, (0, 0, 0))
        text_rect = text_surface.get_rect(center=rectIndex.center)
        screen.blit(text_surface, text_rect)

        selected = selection(event, rectIndexPosition, rectIndexWidth, rectIndexHeight, song)
        if selected is not None:
            globals.selectedSong = selected
            print(globals.selectedSong)
    ...

使用形式:

globals.selectedSong
,然后首先引用模块
globals
,然后引用该模块内的变量。现在,每个以这种方式引用
globals
的模块都将引用 selectedSong
same
实例。

© www.soinside.com 2019 - 2024. All rights reserved.