我相信这是一个非常愚蠢的问题。但我已经使用了python一年,我经历了“学习Python的艰难之路”,我通过了Zetcode WxPython教程,我浏览了WXPython TextCtrl演示,并花了2天的时间搜索google的答案这个看似简单的问题,我没有搞清楚。wxPython - 从一个TextCtrl处理输入并将输出发送到另一个TextCtrl?
我所要做的就是制作一个非常简单的测试游戏,其中我有一个输入框,其中用户输入类似“get pineapple”的内容,然后按[ENTER],并将此短语发送给函数使处理请求,然后将输出发送到一个输出框,说“你不能得到菠萝”。由于我在python办公室编了很多东西,并用VBA编写了功能齐全的财务模型和游戏,所以我确信这很容易,但似乎不可能。而WXPython是障碍。
如果我在“框架”或“面板”或TC1或TC2的类对象下放置了一个字符串变量,那么它们在MainLoop中被视为完全陌生和空的,无论我把变量放在哪里未能识别。在TextCtrl1和TextCtrl2下创建函数时使用相同的变量似乎是不可能的,反之亦然。我认为我必须用“event.skip”来做一些事情,或者让层层传递的东西深入到WXPython的层次中,然后再一次将它们拉回来,但是我不确定将它放在哪里或如何放置做这个。
最重要的是,请告诉我如何找出自己的问题的答案!我感到羞辱只是问它,因为它看起来一定很容易,因为如果它很难,它会存在并在这个Q & A网站上得到回答。
我有所有的GUI骨架看起来不错,我有“Enter”按键事件工作,我有我的多媒体设置和工作正常,我知道我将如何设置我的对象,我只需要指出在这里正确的方向。即使你只是告诉我如何从一个文本控件输入,并将其无变化地传递到输出只读文本控件,这将是完美的,我可以找出其余的。我可以在这里发布我的代码的其余部分,但我读了一些不礼貌的地方。
编辑:我粘贴代码在这里的应答者的请求。
预先感谢您。
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()
很棒!我能够很容易地完成并运行处理来自所有wx面板/类别废话之外的变量的文本:[[我会在这里粘贴光荣的结论,但是这个网站不会让我!]]谢谢一个Werner! – Zaorish9 2014-11-06 12:20:27