为什么我的 python 脚本 运行 正常,但在我尝试跟踪它时却没有?

Why does my python script run normally, but not when i'm trying to trace it?

我正在用 pygame 制作游戏,但我 运行 遇到了一些问题。游戏经常无缘无故地变慢,所以我尝试使用 python 内置的跟踪模块来跟踪它。然而,出于某种原因,当我使用

正常执行它时,它工作得很好
py  main.py

但是当我这样做的时候

py -m trace --trace main.py

它给我以下错误:

File "F:\Downloads\Tanks (2)\Tanks\Tanks\Scripts\render.py", line 13, in load_UI
play_online_button = pgGui.elements.UIButton(pg.Rect((362  * main.res_multiplier, 200 * main.res_multiplier), (300 * main.res_multiplier, 60 * main.res_multiplier)), "Speel Online", main.UI_manager)
AttributeError: module '__main__' has no attribute 'res_multiplier'

虽然,如下所示,属性 res_multiplier 显然是在 load_UI() 运行之前创建的。这可能是因为我正在导入其他脚本吗?

相关代码如下:

import Scripts.render as render

def start_game():
    WIDTH = 1024
    HEIGHT = 576
    
    global res_multiplier 
    res_multiplier = WIDTH / 1024

    #I skipped a bunch of stuff here

    render.load_UI()

    while running:
        #main game loop

if __name__ == __main__:
    start_game()

以及相关函数:

def load_UI():
    global fullscreen
    fullscreen = False
    
    #Startmenu
    global play_local_button, play_online_button, res_menu, quit_button
    play_online_button = pgGui.elements.UIButton(pg.Rect((362  * main.res_multiplier, 200 * main.res_multiplier), (300 * main.res_multiplier, 60 * main.res_multiplier)), "Speel Online", main.UI_manager)
    play_local_button = pgGui.elements.UIButton(pg.Rect((362  * main.res_multiplier, 290 * main.res_multiplier), (300 * main.res_multiplier, 60 * main.res_multiplier)), "Speel Lokaal", main.UI_manager)
    res_menu = pgGui.elements.UIDropDownMenu(["1920x1080","1600x900","1366x768","1280x720", "1024x576"], "1024x576", pg.Rect((700  * main.res_multiplier, 20 * main.res_multiplier), (300 * main.res_multiplier, 30 * main.res_multiplier)), main.UI_manager)
    quit_button = pgGui.elements.UIButton(pg.Rect((0  * main.res_multiplier, 0 * main.res_multiplier), (140 * main.res_multiplier, 40 * main.res_multiplier)), "Spel verlaten", main.UI_manager)
    
    #Onlinemenu
    global ip_text_entry, port_text_entry, connect_button, back_button, connect_error_text, client_port_text_entry
    ip_text_entry = pgGui.elements.UITextEntryLine(pg.Rect((362  * main.res_multiplier, 200 * main.res_multiplier), (300 * main.res_multiplier, 20 * main.res_multiplier)), main.UI_manager)
    ip_text_entry.set_text("127.0.0.1")
    port_text_entry = pgGui.elements.UITextEntryLine(pg.Rect((362  * main.res_multiplier, 240 * main.res_multiplier), (300 * main.res_multiplier, 20 * main.res_multiplier)), main.UI_manager)
    port_text_entry.set_text("1234")
    client_port_text_entry = pgGui.elements.UITextEntryLine(pg.Rect((362  * main.res_multiplier, 280 * main.res_multiplier), (300 * main.res_multiplier, 20 * main.res_multiplier)), main.UI_manager)
    client_port_text_entry.set_text("1235")    
    connect_button = pgGui.elements.UIButton(pg.Rect((362  * main.res_multiplier, 320 * main.res_multiplier), (300 * main.res_multiplier, 50 * main.res_multiplier)), "Verbinden & Spelen", main.UI_manager)
    back_button = pgGui.elements.UIButton(pg.Rect((0  * main.res_multiplier, 0 * main.res_multiplier), (260 * main.res_multiplier, 40 * main.res_multiplier)), "Terug naar het startmenu", main.UI_manager)
    connect_error_text = pgGui.elements.UITextBox("We kunnen geen verbinding maken met de server. Controleer of de server op het goede ip en poort is ingesteld en of je ze hier goed hebt ingevuld.", pg.Rect((690  * main.res_multiplier, 200 * main.res_multiplier), (300 * main.res_multiplier, 130 * main.res_multiplier)), main.UI_manager)
    hide_list = [ip_text_entry, port_text_entry, connect_button, back_button, connect_error_text, client_port_text_entry]
    for x in hide_list:
        x.hide()

您应该将 res_multiplier 作为参数传递:

#main
    render.load_UI(res_multiplier)

#render
def load_UI(res_multiplier):
    global fullscreen
    fullscreen = False
    
    #Startmenu
    global play_local_button, play_online_button, res_menu, quit_button
    play_online_button = pgGui.elements.UIButton(pg.Rect((362  * res_multiplier, 200 * res_multiplier), (300 * res_multiplier, 60 * res_multiplier)), "Speel Online", main.UI_manager)