首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >wxPython -处理来自一个TextCtrl的输入并将输出发送到另一个TextCtrl?

wxPython -处理来自一个TextCtrl的输入并将输出发送到另一个TextCtrl?
EN

Stack Overflow用户
提问于 2014-11-06 04:25:23
回答 2查看 3.2K关注 0票数 0

我相信这是一个非常愚蠢的问题。但是我使用python已经一年了,我读了“艰难地学习Python”,我读了Zetcode WxPython教程,我查看了WXPython TextCtrl演示,我花了两天时间搜索这个看似简单的问题的答案,但我没有弄明白。

我所要做的就是制作一个非常简单的测试游戏,在这个游戏中,我有一个输入框,用户输入类似“获取菠萝”之类的内容,然后按ENTER键,然后将这个短语发送到我将处理请求的函数,然后将输出发送到一个输出框,该输出框上写着“你不能得到菠萝”。由于我在python的办公室编写了很多东西,并且用VBA编写了功能齐全的金融模型和游戏,我确信这会很容易,但这似乎是不可能的。而WXPython是障碍。

如果我将一个字符串变量放在“框架”或“面板”下,或者在TC1或TC2的类对象下,那么它们在MainLoop中被视为完全陌生和空的,不管我把变量放在哪里,它都是不被识别的。在TextCtrl1和TextCtrl2下创建函数时,似乎不可能使用相同的变量,反之亦然。我认为我必须用"event.skip“来做一些事情,或者把东西深深地传递到WXPython的层中,然后再把它们拉回来,但是我不知道该把它放在哪里,也不知道怎么做。

最重要的是,请告诉我,我如何能够自己找出这样的问题的答案!问我觉得很丢脸,因为它看起来一定很容易,因为如果它是困难的,它就会存在,并在这个问答网站上得到答复。

我有所有的GUI骨架看起来很好,我有“进入”按键事件工作,我有我的多媒体设置和工作良好,我知道我将如何设置我的对象,我只是需要指出正确的方向。即使您可以向我展示如何从一个textctrl中获取输入,并将其不改变地传递到输出只读textctrl,这也是完美的,我可以解决其余的问题。我可以在这里发布我剩下的代码,但是我在某个地方读到了这不礼貌的地方。

编辑:我是粘贴在这里的要求,希望答案这里。

提前谢谢你。

代码语言:javascript
运行
复制
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()
EN

Stack Overflow用户

回答已采纳

发布于 2014-11-06 09:28:39

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

在您的代码中,您缺少了style=wx.TE_PROCESS_ENTER

代码语言:javascript
运行
复制
# -*- 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
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26771706

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档