使用空格而不是Python中的选项卡时奇怪的错误

技术标签: Python  标签  pygame.  空间

我正在使用Pygame的简单GUI。在某些时候,我被引起的常量缩进误差混合,因此我决定使用VI用4个空格替换所有选项卡。之后,我会出现一些关于pygame.font.sysfont未被初始化的错误,即使是之前。当然,我认为这与我有关,只是将标签更改为空格,所以我确保一切正确缩进等。我注释出95%的代码,并开始与旧版本的代码进行比较,从旧版本复制行代码到新的。由于它们似乎是相同的(使用CAT -A File.py来比较不可见角色)。

我终于发现这是罪魁祸首: 麻烦制造者

这是两个文件之间唯一不同的东西(这不是三引号)。更改此行有一个标签确实解决了问题。所以,解决问题,我猜。

我的问题是:这是怎么可能的?不应该比标签不容易出错的空间

代码如下所示:

import pygame
pygame.init()

class GameMenu():
    def __init__(self, screen, items, bg_color=(237,237,223), font="Verdana", font_size=30,
                font_color=(237, 28, 36)):
        self.screen = screen
        self.scr_width = self.screen.get_rect().width
        self.scr_height = self.screen.get_rect().height

        self.bg_color = bg_color
        self.clock = pygame.time.Clock()

        self.items = items
        self.font = pygame.font.SysFont(font, font_size)
        self.font_color = font_color
        """
        rest of the code commented out
    """    
    pygame.quit()

if __name__ == "__main__":
    screensize = 0
    screen = pygame.display.set_mode((640, 480), screensize, 32)

    menu_items = ('1', '2', '3', '4', '5')

    pygame.display.set_caption('numbers')
    pygame.mouse.set_visible(True)
    gm = GameMenu(screen, menu_items)

我在这里遗漏了什么?为什么要在Pygame.quit()工作中的选项卡,但没有4个空格,它给出了“Pygame.Error:font未初始化”

编辑: 这是回溯

Traceback (most recent call last):
  File "testMenu.py", line 168, in <module>
      gm = GameMenu(screen, menu_items)
        File "testMenu.py", line 31, in __init__
            self.font = pygame.font.SysFont(font, font_size)
              File "/usr/lib/python2.7/dist-packages/pygame/sysfont.py", 
              line 614, in SysFont
                  return constructor(fontname, size, set_bold, 
                  set_italic)
                    File 
                    "/usr/lib/python2.7/dist-packages/pygame/sysfont.py", line 537, in font_constructor
                        font = pygame.font.Font(fontpath, size)
                        pygame.error: font not initialized

另请注意,Pygame.font.init()不需要do pygame.font.init(),查看 https://www.pygame.org/docs/tut/importinit.html.

看答案

如果你放了 pygame.quit() 没有标签,那么你有这样的东西

(你有没有重要 class 之间 init()quit() - 它同样的工作方式)

import pygame

pygame.init()
pygame.quit()

class ...

if __name__ == "__main__":
    screensize = 0
    screen = pygame.display.set_mode((640, 480), screensize, 32)

    menu_items = ('1', '2', '3', '4', '5')

    pygame.display.set_caption('numbers')
    pygame.mouse.set_visible(True)
    gm = GameMenu(screen, menu_items)

所以你是init. pygame 然后在你退出 pygame - 当您尝试使用某些元素时,您会收到错误 pygame (像 font).

你应该在里面做到这一点 if __name__ == "__main__": 在正确的地方。

import pygame

class ...

if __name__ == "__main__":

    # starts program
    pygame.init()

    screensize = 0
    screen = pygame.display.set_mode((640, 480), screensize, 32)

    menu_items = ('1', '2', '3', '4', '5')

    pygame.display.set_caption('numbers')
    pygame.mouse.set_visible(True)
    gm = GameMenu(screen, menu_items)

    # ... other code ...

    # ends program
    pygame.quit()

顺便提一句: 这可能更有用(而不是 scr_width , scr_height)

self.screen_rect = self.screen.get_rect()

因为你总是可以得到 self.screen_rect.width, self.screen_rect.height 并且 self.screen_rect.center (屏幕上的中心元素),或其他。


智能推荐

类似选项卡的分页操作

很多小伙伴都知道选项卡切换的这一个功能,那么呢 我又来介绍一下我自己创作类似于选项卡分页的操作。我用的开发工具是Visual Studio 2015,这个开发工具是十分流行的。废话不说。接下来就跟着我开始吧 首先第一步就是打开Visual Studio 2015。 接着就是创建一个方法以视图的形式返回。 接着我们要搭建页面。因为这一步有点重要所以要详细说一下 我们要做的是页面分为三个部分 就比如说...

Android选项卡Tab的实现

Android选项卡的实现,首先需要我们创建一个TabHost 在XML文件进行配置 <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android...

The Design of Everyday Things 读书笔记

  近日抽闲读了老师推荐已久的这部设计史上的著作——唐纳德诺曼的The Design of Everyday Things,中文译名是《设计心理学》,全套共四部,我读了第一部,总的感觉就一个词——恍然大悟!被作者的细心以及擅于思考所折服,他能够抓住生活中很多人习以为常的不方便和小错误,从而解释糟糕的设计可能带给我们的麻烦甚至灾难。...

博客,记录一点一滴,终将汇聚成星辰大海

博客,记录一点一滴,终将汇聚成星辰大海 目录 写博客的好处: 要怎么写博客: 写博客的好处: 【1】我认为写博客能充当自己的资料库,你能随时翻阅你之前学的所有东西,你是否有这样的经历,明明自己之前学过这方面的内容,而且还挺熟悉,但怎么都记不起来具体细节,这时候你的资料库就能最快帮你回忆起你学的东西。 【2】把自己学到的经验,分享出来,自己之前踩到的所有坑都记录下来,当读者读你的博客时,就能少走一些...

一个好玩的工具BeEF介绍及未来要实验

beef BeEF-XSS是一款非常强大的web框架攻击平台,集成了许多payload,可以实现许多功能 BeEF-XSS可以说是最强大的XSS漏洞利用工具,可以收集浏览器信息、键盘记录、社会工程等 总之就是使用beef这个工具来利用xss漏洞的 beef安装 我在kail虚拟机上安装的,很简单的在终端输入一句命令sudo apt setup beef-xss,然后安装成功设置密码就行了,因为之后...

猜你喜欢

IDEA学习篇——findbugs 插件 下载、安装、使用详解

首先我下载的版本是 FindBugs-IDEA-1.0.1 ,然后我会在文中提供(FindBugs-IDEA-1.0.1.zip)下载资源,我在安装过程中的问题如下: 1,在编辑器里面始终下载不下来,每次都是一半的时候就失败啦。最后就去网上下载zip文件本地安装。 下面就是下载安装使用一条龙服务详情 先是在这个编辑器里面怎么安装这个插件的示范。也许你网络很顺畅呢,就OK拉。 我是等到花儿都谢啦,也...

块级格式化上下文

块级格式化上下文听说过多次,自己也去看了许多文章,但自己总是觉得晦涩,搞不清楚,后来又再去查了下很多文章,终于觉得自己理解一些了,希望自己描述出来能够准确和易懂。 Formatting context Formatting context 是页面的一个渲染区域,它有着自己的渲染规则,决定渲染区域里面的元素怎么定位,以及里面的元素之间的相互作用。而块级格式化上下文(Block fomatting c...

meter资源监控器开发——关键代码分析

代码分析也无需事无巨细皆列而剖之,只要找到关键所在也就是了;又不然列一堆的声明上来,纵然有人有耐心看下去,我也没耐心写下去啊。特别关注了三 个类,Stats、MonitorPerformancePanel、MonitorGraph。分别是获取解析得到的数据、监控器面板显示和监视器上的 图像绘制。下面选取了一些关键代码来进行分析: 首先是Stats.java,下面是计算内存使用率的方法 public...

fiddler对手机https抓包

1、将手机和fiddler连接成功之后 2、用手机浏览器访问fiddler地址与端口,IP:8888  3、访问之后,点击图中按钮,IOS信任后安装,android信任后下载 4、IOS安装后查看fiddler是否可以抓到https的包,如果不能,那是因为IOS系统版本较高的原因,进入手机手机设置-通用-关于本机,最后一个受信任证书存储区里面,把fiddler的证书加入信任,这样再次查看...

3d打印模型为什么文件格式必须是stl和stp的?

https://www.sohu.com/a/197115674_425589   3D打印需要有3D立体图,3D立体图有很多格式,不同的软件做出来的格式是不同的。比如常见的3D打印格式有:STL、STP、IGS、OBJ、BREP、MAX、3DM、3DS、X_T、SKP、SLDPRT、PRT、ASM、F3D、FBX、RVT、WIRE等等。格式不同,其他软件可能就打不开。同样的道理,3D打...

问答精选

Applying stats.percentileofscore to every row by column

df= I need a new column with the percentile score for each element with respect to the column. The final answer should look like this. I want the output of the stats.percentileofscore() function to be...

C - Creating node with multiple children by function - segmentation fault

I want to create tree with multiple children and create every node with a function. Here is my structure for node: And here is my function to create new node (with four children): When I run this func...

How to get all secrets in one call Azure key vault

I am using sample code explain here https://github.com/Azure-Samples/app-service-msi-keyvault-dotnet but they only explained how can we get single secrete not list of secrete. so to get all secrete I'...

increase eclipse IDE font size

I am using Eclipse I would like to increase the font size of the IDE, not just the font of the editor. Is there any way I can do that? I haven't been able to find a solution for this yet. you can try ...

Generating Guid by database automatically

It regards Entity Framework 6 but maybe it isn’t related to the version. I have some class which has Guid Id but by inheritance. Right now I would like to change this model to auto generate Id b...

相关问题

相关文章

热门文章

推荐文章

相关标签

推荐问答