Python的字符串问题

时间:2013-02-01 14:25:58

标签: python string wxpython

我是一个Python菜鸟,所以我可能会在这里遗漏一些东西,但我在程序中如何处理字符串时遇到问题。当我显示它时,只显示第一个字符。

# some code
MessageBox = ctypes.windll.user32.MessageBoxA
# some other code
testString = self.statusBar1.GetStatusText(0)
# displays "azertyu"
MessageBox(None, "azertyu", 'COUCOU', 0)
# displays 'M'
MessageBox(None, testString, 'COUCOU3', 0)
# displays 'a'
MessageBox(None, testString[1:], 'COUCOU3', 0) #
#displays 'c'
MessageBox(None, testString[2:], 'COUCOU3', 0)

完整的字符串是' Machine' (它实际上比那长)。 Python如何认为任何字符都是结尾字符并且一次只显示一个字符?我在这里错过了一些Python基础知识吗?

PS。 GetStatusText参考位于http://www.wxpython.org/docs/api/wx.StatusBar-class.html#GetStatusText。我用一个很长的字符串测试了GetStatusText,它似乎没有剪切文本。

4 个答案:

答案 0 :(得分:5)

MessageBoxA是MessageBox win32 API的ascii版本。您的testString可能是一个Unicode值,因此传递给MessageBoxA的值最终看起来像一个字节数组,每个其他索引都为零。换句话说,它看起来像一个字符串,只有一个字符以NULL字符结尾。我打赌如果你使用str(testString)或切换到MessageBoxW然后它将按预期工作,但你真的应该使用wx.MessageBox或wx.MessageDialog。

答案 1 :(得分:1)

如果您使用的是wxPython,为什么要尝试显示带有ctypes的消息框? wxPython包有自己的消息对话框。请参阅以下链接:

wxPython演示包(可从wxPython网站下载)包含MessageDialog和GenericMessageDialog的示例。

答案 2 :(得分:0)

它将testString视为列表

In [214]: for x in "Machine":
   .....:     print x
   .....:
M
a
c
h
i
n
e

你试过吗?

MessageBox(None, [testString], 'COUCOU3', 0)

好像MessageBox期待一个txt列表,这可能是有道理的:

["DANGER", "Will Robinson"]

然后会在你的消息上提供两行txt。

PURE GUESSWORK

答案 3 :(得分:0)

尝试用 ctypes.windll.user32.MessageBoxW 代替 ctypes.windll.user32.MessageBoxA

import ctypes
ctypes.windll.user32.MessageBoxW(None, "Hello, world!", "Test", 0)