【问题标题】:set a value to a textCtrl(string) from another TextCtrl ( item[])in wxpython从 wxpython 中的另一个 TextCtrl ( item[]) 为 textCtrl(string) 设置一个值
【发布时间】:2015-06-15 15:10:28
【问题描述】:

我正在用另一个 TextCtrl(name:controlz) 替换 TextCtrl(name:control) 的值。到目前为止,我已经做到了。

def replace(self, event):
    newtext = tokenize_editor_text(self.controlz.GetValue())
    text = tokenize_editor_text(self.control.GetValue())
    for word in text:

        if (word == misspelled_list[0]):
            text[text.index(word)] = newtext
            corrected = ' '.join(map(str,text))
            self.control.SetValue(corrected)
            print corrected

例如,当(self.controlz) 中有一个输入( Chony) 并在 (self.control) 中替换另一个单词(choni) 时,它会替换[u'chony']。即使我使用过' '.join(map(str,text))。我怎样才能得到相同的值 ( chony ) 不是 [u'chony'] 任何解决方案表示赞赏。 非常感谢

【问题讨论】:

  • 目前有什么问题?
  • 我怎样才能得到相同的值(chony)而不是[u'chony']??? @JoranBeasley
  • 亲爱的@JoranBeasley,当我使用 .' 'join(map(str,text)) 它只去除第一个 []。我怎样才能摆脱 [u'chony'] ???
  • 看我答案的最后一行...

标签: python python-2.7 wxpython


【解决方案1】:

如果您在列表或元组上调用 str,它会显示列表内部值的 repr

str(["1","2"]) #==> '[u"1",u"2"]'

这个电话

newtext = tokenize_editor_text(self.controlz.GetValue())

返回一个我相信类似[u"chony"]的列表

然后将该列表值设置为内部的列表索引

text[i] = newtext #==> text[i] = [u"chony",]  ... => text = [[u"chony"],[...]]

稍后你调用每个文本项的字符串

map(str, text) # which calls str on the array that you set as the indexes 

如果你在newtext 中总是只有一个元素,你可以分配第 0 个索引

text[i] = newtext[0]

【讨论】:

  • 亲爱的@Joran Beasley,当我使用 .' 'join(map(str,text)) 它只去除第一个 []。我怎样才能摆脱 [u'chony'] ???
猜你喜欢
  • 1970-01-01
  • 2023-03-29
  • 2015-01-08
  • 1970-01-01
  • 2010-09-25
  • 1970-01-01
  • 2010-11-17
  • 2012-07-01
  • 1970-01-01
相关资源
最近更新 更多