简体   繁体   English

在TkMessageBox中显示数组的元素

[英]Show elements of an array in TkMessageBox

The code i'm using is the following: 我正在使用的代码如下:

if len(areUnusedParams) > 0:
   tkMessageBox.showinfo('Error!','The following parameters are unchanged throughout the C-files and are also not present in parameter.txt:\n')

Now, i want the contents of areUnusedParams (which is an array) to be posted when the MessageBox is displayed and every element should be on their own line. 现在,我希望在显示MessageBox时发布areUnusedParams (是一个数组)的内容,并且每个元素都应该在自己的行上。 I was contemplating something like: 我正在考虑类似的东西:

'\n'.join(areUnusedParams)

But I don't know how to implement it, PyCharm keeps complaining when I try to do it. 但是我不知道如何实现,当我尝试这样做时,PyCharm总是抱怨。

when you add the list of areUnusedParams to showinfo as a third argument it will raise a TypeError much like the following: 当将areUnusedParams列表添加到showinfo作为第三个参数时,它将引发TypeError ,如下所示:

Traceback (most recent call last):
  File ".../test.py", line 3, in <module>
    tkMessagebox.showinfo("title","message","thing")
TypeError: showinfo() takes from 0 to 2 positional arguments but 3 were given

Since you intend the '\\n'.join(areUnusedParams) to be part of the message you need to add it to the message instead of passing as an additional argument: 由于您打算将'\\n'.join(areUnusedParams)消息的一部分,因此需要将其添加到消息中,而不是作为附加参数传递:

tkMessageBox.showinfo('Error!',
                      'The following parameters are unchanged throughout the C-files and are also not present in parameter.txt:\n' \
                         + '\n'.join(areUnusedParams))

I assume that all of areUnusedParams are all strings but just to be thorough, if they are not then it is an invalid thing to pass to str.join in which case you can cast all of them to strings using map : 我假设所有的areUnusedParams都是字符串,但要透彻一点,如果不是,那么传递给str.join是无效的,在这种情况下,您可以使用map将它们全部转换为字符串:

'\n'.join(map(str,areUnusedParams))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM