我相信這是一個非常愚蠢的問題。但我已經使用了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