我确信这是一个非常愚蠢的问题。但是我使用 python 已经一年了,我经历了“Learn Python the hard way”,我经历了 Zetcode WxPython 教程,我浏览了 WXPython TextCtrl Demo,我花了 2 天时间在谷歌上寻找答案这个看似简单的问题,我没有弄清楚。
我要做的只是制作一个非常简单的测试游戏,其中有一个输入框,用户在其中输入“get pineapple”之类的内容,然后按 [ENTER],然后将此短语发送到我将制作的函数处理请求,然后将输出发送到一个输出框,上面写着“你买不到菠萝”。由于我在办公室用 python 编写了很多东西,并且用 VBA 编写了功能齐全的金融模型和游戏,我确信这很容易,但似乎不可能。WXPython 是障碍。
如果我在“框架”或“面板”下或 TC1 或 TC2 的类对象下放置一个字符串变量,那么它们在 MainLoop 中被视为完全陌生和空,无论我将变量放在哪里都无法识别。在 TextCtrl1 和 TextCtrl2 下创建函数时似乎不可能使用相同的变量,反之亦然。我想我必须用“event.skip”做一些事情,或者可能将多层传递到 WXPython 的层中,然后再把它们拉回来,但我不知道把它放在哪里或如何做这个。
最重要的是,请告诉我如何自己找出此类问题的答案!只是问它我感到很丢脸,因为它看起来一定很容易,因为如果它很难,它就会存在并在这个问答网站上得到回答。
我的所有 GUI 骨架看起来都很好,我的“Enter”按键事件正常工作,我的多媒体设置并且工作正常,我知道我将如何设置我的对象,我只需要指向正确的方向这里。即使您可以向我展示如何从一个 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()