为什么我的 python 脚本可以正常运行,但在我尝试跟踪它时却不能?

曲奇刀

我正在用 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)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么从 PowerShell 运行的 Perl 脚本可以在新控制台中运行,而 Python 脚本却不能?

为什么此脚本可以从命令行运行,但是当我将其放入Windows批处理文件中时却不能运行?

为什么我的 bash 脚本可以在终端中工作,而在使用 crontab 时却不能?

为什么我的Erlang引导脚本可以从控制台运行,但是从init系统(sysvinit,upstart,systemd)运行时却不能运行?

为什么我的HTML按钮不能运行我的Python脚本?

为什么php脚本可以在免费托管上工作,而在我的vps上却不能工作?

为什么一个简单的 CSV 清理 python 脚本可以工作,而另一个却不能?

为什么此C代码在调试时能正常运行,但在正常运行时却不能正常运行?

为什么我可以进场却不能进场?

为什么我的循环在第一次迭代时能正常工作,但在我循环的整个集合上却不能正常工作?

为什么我的admob插页式广告在xcode模拟器中可以正常运行,但在我的测试设备(iphone 4s和ipod 5th gen)上却不能正常工作?

为什么我的python脚本似乎可以正常工作,但为什么却出现500错误?

为什么我不能安装python包?

为什么我的使用twilio的脚本可以在本地完美运行,但是在Python Anywhere上引发错误?

为什么我的Groovy脚本在Jenkins脚本控制台中运行却不能作为Groovy Postbuild脚本运行?

批处理文件在我的桌面上可以正常工作,但在虚拟桌面上却不能。为什么?

python selenium-为什么我不能单击它?

为什么我的 for 循环在 Python 中不能正常工作?

为什么我的python脚本不能更改umask?

为什么我的硒不能在python脚本中工作?

Python - aiohttp - 为什么我的测试不能运行?

为什么我的while函数不能在Python上运行?

为什么任务计划程序不会运行我的 Python 脚本

为什么我在Python中收到BadRequestKeyError?

为什么我无法插入Python列表?

为什么我的python函数被跳过?

为什么我的 Python venv 目录是只读的?

为什么我的python while无限循环?

为什么python字典给我Keyerror?