wxPython - 从一个TextCtrl处理输入并将输出发送到另一个TextCtrl?

时间:2014-11-06 04:25:24

标签: python user-interface event-handling wxpython textctrl

我确信这是一个非常愚蠢的问题。但我已经使用了python一年了,我经历了#34;艰难学习Python",我浏览了Zetcode WxPython教程,我浏览了WXPython TextCtrl演示,并且我已经完成了花了2天谷歌搜索这个看似简单的问题的答案,我没有搞清楚。

我要做的就是制作一个非常简单的测试游戏,我有一个输入框,用户输入类似"获取菠萝",然后按[ENTER],并将此短语发送到我将处理请求的函数,然后将输出发送到输出框,其中显示的内容类似于"您无法获得菠萝。"由于我在python办公室编写了很多东西,并用VBA编写了全功能的财务模型和游戏,我确信这很容易,但似乎不可能。 WXPython是障碍。

如果我把一个字符串变量放在" frame"或者"小组"或者在TC1或TC2的类对象下,它们在MainLoop中被视为完全异类和空洞,无论我把变量放在哪里都不被识别。在TextCtrl1和TextCtrl2下创建函数时似乎不可能使用相同的变量,反之亦然。我想我必须做一些事情" event.skip",或者可能在WXPython的各个层面深入传递,然后再将它们拉回来,但我不确定在哪里把它或如何做到这一点。

首先请告诉我如何自己找出这样的问题的答案!我觉得羞辱只是问它,因为它似乎一定很容易,因为如果它很难存在并在这个Q& A网站上得到解答。

我所有的GUI骨架看起来都很好,我有"输入"按键事件工作,我的多媒体设置和工作正常,我知道我将如何设置我的对象,我只需要在这里指出正确的方向。即使你可以告诉我如何从一个textctrl获取输入,并将其保持原样传递给输出只读textctrl,这将是完美的,我可以弄清楚其余的。我可以在这里发布我的其余代码,但我读到了某些不礼貌的地方。

编辑:我是在一位潜在的回答者的要求下粘贴代码的。

提前谢谢。

import wx
from random import randint
from pygame import mixer # Load the required library


mixer.init()
mixer.music.load('sog.ogg')
mixer.music.set_volume(1.0)
mixer.music.play()

RandomSound1 = mixer.Sound('CritHit.wav')
RandomSound2 = mixer.Sound('swallow2.wav')
RandomSound3 = mixer.Sound('thunder2.wav')

#instantiate class

class MusTogButt(wx.Button):

    def __init__(self, *args, **kw):
        super(MusTogButt, self).__init__(*args, **kw)

        self.Bind(wx.EVT_BUTTON, self.MusicToggleFunction)

    def MusicToggleFunction(self, mtf):
        if mixer.music.get_busy() == True:
            print "test, is playing"
            mixer.music.stop()
        else:
            mixer.music.play()

class SoundTestButt(wx.Button):

    def __init__(self, *args, **kw):
        super(SoundTestButt, self).__init__(*args, **kw)

        self.Bind(wx.EVT_BUTTON, self.PlayRandSound)

    def PlayRandSound(self, mtf):
        randsoundnum = randint (0,100)
        if randsoundnum < 34:
            RandomSound1.play()
        elif randsoundnum  < 68:
            RandomSound2.play()
        else:
            RandomSound3.play()


class CPFInputter(wx.TextCtrl):

    def __init__(self, *args, **kw):
        super(CPFInputter, self).__init__(*args, **kw)

        self.Bind(wx.EVT_COMMAND_ENTER, self.TextEntryFunction)

    def TextEntryFunction(self, mtf):
        print "you pressed enter"


class Example(wx.Frame):

    def __init__(self, parent, title):
        super(Example, self).__init__(parent, title=title,
            size=(800, 600))


        self.InitUI()
        self.Centre()
        self.Show()


    def InitUI(self):

        panel = wx.Panel(self)

        #--------------------------------------------------------------
        #This is an event handler. It handles the pressing of Enter, only.
        def UserInputtedCommand(self):
            keycode = self.GetKeyCode()
            if keycode == wx.WXK_RETURN or keycode == wx.WXK_NUMPAD_ENTER:
                print self.GetValue()

        hbox = wx.BoxSizer(wx.HORIZONTAL)

        fgs = wx.FlexGridSizer(3, 2, 9, 25)
        #  3 rows
        #  2 columns
        #  9 vert gap
        # 25 horizonatl gap

        OutBoxLabel = wx.StaticText(panel, label="Outbox") #notice that these do NOT have wx.expand and they do NOT expand when the window is sized.
        InBoxLabel = wx.StaticText(panel, label="Input:") #notice that these do NOT have wx.expand and they do NOT expand when the window is sized.

        #make a bunch of input text controls, under the main panel.
        InTC = wx.TextCtrl(panel)
        InTC.Bind(wx.EVT_KEY_DOWN, UserInputtedCommand)
        OutTC = wx.TextCtrl(panel, style=wx.TE_MULTILINE)
        MusicToggle = MusTogButt(panel, label="Toggle Music Playback")
        SoundTester = SoundTestButt(panel, label="Play Random Sound")

        #Use AddMany AFTER you've built all your widgets with their specifications and put them in objects.
        fgs.AddMany([(OutBoxLabel), (OutTC, 1, wx.EXPAND),
            (InBoxLabel), (InTC, 1, wx.EXPAND), (MusicToggle), (SoundTester)])

        fgs.AddGrowableRow(0, 1)
        fgs.AddGrowableCol(1, 1)
        # So, in other words, the 1st and second textboxes can grow horizontally, and the 3rd and final textbox can grow horizontally and vertically.

        #lastly, add the FGS to the main hbox.
        hbox.Add(fgs, proportion=1, flag=wx.ALL|wx.EXPAND, border=15)
        #...and set sizer.
        panel.SetSizer(hbox)


if __name__ == '__main__':

    app = wx.App()
    Example(None, title='CPF_FunGame2_MediaTest')
    print "cpf_Fungame2_mediatest_running"
    app.MainLoop()

2 个答案:

答案 0 :(得分:1)

下面创建两个文本控件,当您在第一个控件中键入内容时按下输入它将显示在第二个控件中。

在您的代码中,您错过了style=wx.TE_PROCESS_ENTER

# -*- coding: utf-8 -*-
#!/usr/bin/env python
import wx
import wx.lib.sized_controls as sc

class AFrame(sc.SizedFrame):
    def __init__(self, *args, **kwds):
        super(AFrame, self).__init__(*args, **kwds)

        pane = self.GetContentsPane()

        self.tc1 = wx.TextCtrl(pane, style=wx.TE_PROCESS_ENTER)
        self.tc2 = wx.TextCtrl(pane)

        self.tc1.Bind(wx.EVT_TEXT_ENTER, self.onTc1Enter)

    def onTc1Enter(self, Evt):
        self.tc2.ChangeValue(self.tc1.GetValue())



if __name__ == "__main__":
    import wx.lib.mixins.inspection as WIT

    app = WIT.InspectableApp()
    f = AFrame(None)
    f.Show()
    app.MainLoop()

答案 1 :(得分:0)

我怀疑您的问题与变量的范围有关,但没有更多细节,我只能推测问题可能是什么。您可以通过以下几种方式处理:

  1. 将输出框的对象传递给此函数,然后您可以使用其方法显示结果。
  2. 返回该值以允许其在比原来更大的范围内使用。
  3. 绑定到TextCtrl时,请尝试以下操作:

    self.InTC.Bind(wx.EVT_TEXT, UserInputtedCommand)
    
    def UserInputtedCommand (self, event):
        Line = self.InTC.GetValue()