python错误:TypeError:需要一个整数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27663567/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:04:52  来源:igfitidea点击:

python error: TypeError: an integer is required

pythonpython-2.7

提问by

I'm working on my python script as I'm stored the list of elements in the arrays.

我正在处理我的 python 脚本,因为我将元素列表存储在数组中。

I have got a very weird error when I'm trying to put each element in the getControlto allowed me to change the width size. I can get the list of elements from the arrays without have any problem.

当我尝试将每个元素放入getControl允许我更改宽度大小时,我遇到了一个非常奇怪的错误。我可以毫无问题地从数组中获取元素列表。

When I try this:

当我尝试这个时:

programs_width = list()
for pos_X, prog_width, prog_ids in zip(program_X, program_width, program_id):
    if pos_X == '1073':    
        #30 mins
        if prog_width == '342':
            programs_width = 181

        #1 hour
        if prog_width == '691':
            programs_width = 181

        #need to find out how to get the programs_width outside
        print programs_width
        print prog_ids
        self.getControl(prog_ids).setWidth(programs_width)

It give me an error:

它给了我一个错误:

01:03:24 T:2084   ERROR: Exception in thread All_Channels_BACKUP_thread:
Traceback (most recent call last):
File "C:\Program Files (x86)\XBMC\system\python\Lib\threading.py", line 532, in __bootstrap_inner
self.run()
File "C:\Users\user\AppData\Roaming\XBMC\addons\script.tvguide\test.py", line 8158, in run
self.xtarget()
File "C:\Users\user\AppData\Roaming\XBMC\addons\script.tvguide\test.py", line 1410, in All_Channels_BACKUP                                                   self.getControl(str(prog_ids)).setWidth(str(programs_width))
TypeError: an integer is required

The error are jumping on this line:

错误在这条线上跳跃:

self.getControl(prog_ids).setWidth(programs_width)

Here is a output for the width and ids:

这是宽度和 ID 的输出:

01:13:18 T:5032  NOTICE: 181
01:13:18 T:5032  NOTICE: 3072
01:13:18 T:5032  NOTICE: 181
01:13:18 T:5032  NOTICE: 3211
01:13:18 T:5032  NOTICE: 181
01:13:18 T:5032  NOTICE: 3279
01:13:18 T:5032  NOTICE: 181
01:13:18 T:5032  NOTICE: 3348
01:13:18 T:5032  NOTICE: 181
01:13:18 T:5032  NOTICE: 3417

I got no idea why I have an error, all I'm trying to do is to use the variable prog_idsto get each element from the arrays to put each id in the getcontrolbefore I could use the variable programs_widthto change the width size each time. The type for program_widthis intand the type for prog_idis strwhich is a string.

我不知道为什么我有错误,我想要做的就是使用变量prog_ids从数组中获取每个元素,然后在每次getcontrol使用变量programs_width更改宽度大小之前将每个 id 放入。类型为program_widthint,类型为prog_idstr字符串。

Do you know why I have got an error and do you know how to fix it?

你知道为什么我有一个错误,你知道如何解决它吗?

Edit: I realised that the problem are coming from this line:

编辑:我意识到问题来自这一行:

 elif pos_X == '1073':

Here is the list of elements:

以下是元素列表:

15:34:08 T:5872  NOTICE: 375
15:34:08 T:5872  NOTICE: 724
15:34:08 T:5872  NOTICE: 2804
15:34:08 T:5872  NOTICE: 5226
15:34:08 T:5872  NOTICE: 5924
15:34:08 T:5872  NOTICE: 6273
15:34:08 T:5872  NOTICE: 6622
15:34:08 T:5872  NOTICE: 6971
15:34:08 T:5872  NOTICE: 7320
15:34:08 T:5872  NOTICE: 7669
15:34:08 T:5872  NOTICE: 8018
15:34:08 T:5872  NOTICE: 8716
15:34:08 T:5872  NOTICE: 9065
15:34:08 T:5872  NOTICE: 9414
15:34:08 T:5872  NOTICE: 9763
15:34:08 T:5872  NOTICE: 10112
15:34:08 T:5872  NOTICE: 10461
15:34:08 T:5872  NOTICE: 10810
15:34:08 T:5872  NOTICE: 11159
15:34:08 T:5872  NOTICE: 11508
15:34:08 T:5872  NOTICE: 11857
15:34:08 T:5872  NOTICE: 12206
15:34:08 T:5872  NOTICE: 13937
15:34:08 T:5872  NOTICE: 14635
15:34:08 T:5872  NOTICE: 14984
15:34:08 T:5872  NOTICE: 15333
15:34:08 T:5872  NOTICE: 15682
15:34:08 T:5872  NOTICE: 16031
15:34:08 T:5872  NOTICE: 16380
15:34:08 T:5872  NOTICE: 16729
15:34:08 T:5872  NOTICE: 17078
15:34:08 T:5872  NOTICE: 17427
15:34:08 T:5872  NOTICE: 17776
15:34:08 T:5872  NOTICE: 18125
15:34:08 T:5872  NOTICE: 18474
15:34:08 T:5872  NOTICE: 18823
15:34:08 T:5872  NOTICE: 19172
15:34:08 T:5872  NOTICE: 19521
15:34:08 T:5872  NOTICE: 19870
15:34:08 T:5872  NOTICE: 20219
15:34:08 T:5872  NOTICE: 20568
15:34:08 T:5872  NOTICE: 20917
15:34:08 T:5872  NOTICE: 22648
15:34:08 T:5872  NOTICE: 23346
15:34:08 T:5872  NOTICE: 23695
15:34:08 T:5872  NOTICE: 24044
15:34:08 T:5872  NOTICE: 24393
15:34:08 T:5872  NOTICE: 24742
15:34:08 T:5872  NOTICE: 25091
15:34:08 T:5872  NOTICE: 25440
15:34:08 T:5872  NOTICE: 26138
15:34:08 T:5872  NOTICE: 26487
15:34:08 T:5872  NOTICE: 26836
15:34:08 T:5872  NOTICE: 27185
15:34:08 T:5872  NOTICE: 27534
15:34:08 T:5872  NOTICE: 27883
15:34:08 T:5872  NOTICE: 28232
15:34:08 T:5872  NOTICE: 28581
15:34:08 T:5872  NOTICE: 28930
15:34:08 T:5872  NOTICE: 29279
15:34:08 T:5872  NOTICE: 29628
15:34:08 T:5872  NOTICE: 31359
15:34:08 T:5872  NOTICE: 32057
15:34:08 T:5872  NOTICE: 32406
15:34:08 T:5872  NOTICE: 32755
15:34:08 T:5872  NOTICE: 33104
15:34:08 T:5872  NOTICE: 33453
15:34:08 T:5872  NOTICE: 33802
15:34:08 T:5872  NOTICE: 34151
15:34:08 T:5872  NOTICE: 375
15:34:08 T:5872  NOTICE: 1073
15:34:08 T:5872  NOTICE: 3153

采纳答案by Aran-Fey

The problem is that programs_widthis a list.

问题是这programs_width是一个列表。

programs_width = list()

If none of these ifstatements

如果没有这些if陈述

if prog_width == '342':
    programs_width = 181

#1 hour
if prog_width == '691':
    programs_width = 181

trigger, then programs_widthwill still be a list here:

触发器,那么programs_width这里仍然是一个列表:

self.getControl(prog_ids).setWidth(programs_width)

which causes an error.

这会导致错误。

回答by Michael Aaron Safyan

Your quotes are making these values strings, not integers. Remove the quotes on your values. In addition, the error message clearly indicates the bad line:

您的引号使这些值成为字符串,而不是整数。删除值上的引号。另外,错误信息明确指出坏行:

self.getControl(str(prog_ids)).setWidth(str(programs_width))

Notice that you are explicitly converting the parameter to a string with "str". Use "int", instead:

请注意,您正在将参数显式转换为带有“str”的字符串。改用“int”:

self.getControl(str(prog_ids)).setWidth(int(programs_width))

... or omit the conversion altogeher if you know the input is already an integer.

...或者如果您知道输入已经是整数,则忽略转换。