你好,我是一名業餘程序員。我建立了一個簡單的文本扭曲遊戲,並將其命名爲texttwister_compy.py。我也能夠構建一個簡單的GUI。但我需要學習如何將我的python程序集成到我的wxpython GUI中。 這裏是我的wxPython代碼:如何將python合併到wxpython中
import os
import wx
class MainWindow(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, title=title, size=(200,100))
self.Control=wx.TextCtrl(self, style=wx.TE_MULTILINE)
self.CreateStatusBar()
filemenu=wx.Menu()
editmenu=wx.Menu()
viewmenu=wx.Menu()
toolsmenu=wx.Menu()
menuAbout=filemenu.Append(wx.ID_ABOUT, "&About")
menuExit=filemenu.Append(wx.ID_EXIT, "&Exit")
menuOpen=filemenu.Append(wx.ID_OPEN, "&Open")
menuCopy=editmenu.Append(wx.ID_COPY, "©")
menuClear=viewmenu.Append(wx.ID_CLEAR, "&Clear")
clearButton=wx.Button(self, wx.ID_CLEAR, "Clear")
menuBar=wx.MenuBar()
menuBar.Append(filemenu, "&file")
menuBar.Append(editmenu, "&Edit")
menuBar.Append(viewmenu, "&View")
menuBar.Append(toolsmenu, "&Tools")
self.SetMenuBar(menuBar)
self.Bind(wx.EVT_MENU, self.OnAbout, menuAbout)
self.Bind(wx.EVT_MENU, self.OnExit, menuExit)
self.Bind(wx.EVT_MENU, self.OnOpen, menuOpen)
self.Bind(wx.EVT_MENU, self.OnCopy, menuCopy)
self.Show(True)
def OnAbout(self, event):
devi=wx.MessageDialog(self, "small text editpr", wx.OK)
devi.ShowModal()
devi.Destroy()
def OnExit(self, event):
self.Close(True)
def OnOpen(self, event):
self.dirname=''
dlg=wx.FileDialog(self, "choose file", self.dirname, "*.*", wx.OPEN)
if dlg.ShowModal() == wx.ID_OK:
self.filename = dlg.GetFilename()
self.dirname = dlg.GetDirectory()
f = open(os.path.join(self.dirname, self.filename), 'r')
self.control.SetValue(f.read())
f.close()
dlg.Destroy()
def OnCopy(self, event):
mk=wx.EditDialog(self, "Copy files", wx.OK)
mk=ShowModal()
def OnClear(self, event):
clearButton.ShowModal()
app=wx.App(False)
frame=MainWindow(None, "Hello commander")
app.MainLoop()
我也有關於在wxPython問題演示一個問題,究竟是你打開它們。
我最後的問題是你是如何做到這一點?
所以這裏是爲texttwister方案:
import random
list=[['D','R','P','O','O','L','E','Q','U','A'],['L','A','C','I','T','Y','L','A','N','A'],
['I','S','T','M','E','C','H','Y','R',],['O','C','R','I','G','N','A'],
['M','E','I','D','C','I','E','N'],['N','O','S','S','I','M','I','E'],
['Y','M','E','C','H','L','A'],['D','A','G','R','U'],['I','E','V','D']]
list2=['quadropole', 'analytical', 'alchemy','chemistry', 'organic',
'medicine', 'emission','durga','devi']
random.choice(list)
def operation():
print random.choice(list)
x=operation()
while True:
from itertools import repeat
guess=raw_input('please untwist the word:')
if guess in list2:
print 'CONGRATULATIONS! you got the word'
print 'keep going strong'
repeat(operation())
continue
if guess not in list2:
print 'NO! this is not correct wrong answer please try again'
if guess==raw_input():
print 'Program is CLOSING'
print 'Please have a good day'
print 'hope you enjoyed the game'
break
如何整合這與上面的代碼?所以我的主循環就像是類Loop
的類或函數。那麼在主要的wxpython中,我將它稱爲類還是函數?