【问题标题】:how to combine python into wxpython如何将python结合到wxpython中
【发布时间】:2011-06-23 23:35:49
【问题描述】:

您好,我是一名业余程序员。我构建了一个简单的文本扭曲游戏,并将其命名为 texttwister_compy.py。我还能够构建一个简单的 GUI。但我需要学习如何将我的 python 程序集成到我的 wxpython GUI 中。 这是我的 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, "&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中我是把它称为类还是函数呢?

【问题讨论】:

    标签: python wxpython


    【解决方案1】:

    只需将您的操作事件绑定到您的按钮或鼠标点击。因此,如果您的游戏涉及更改框中的数字并在每次单击时将其递增一个,则只需调用游戏中的函数作为输入并更新 GUI。在该点击上,它不仅应该更新该框,还应该在您的游戏中做一些可能产生后果或奖励的事情,然后这将反映在您的 GUI 上(即更新您的分数)。

    虽然此时您的游戏可能需要采用 OOP 形式,因为程序可能无法很好地解决此问题。

    测试测试用例,首先需要找到示例。对于大多数模块,它们通常位于您的 python-directory/wxpython/examples 中。你会找到它们,并像使用任何其他 python 脚本一样启动它们。通常会有自述文件,wxpython 文档也会告诉你该怎么做。

    【讨论】:

      猜你喜欢
      • 2013-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-06
      • 2011-03-04
      • 1970-01-01
      • 2014-12-02
      相关资源
      最近更新 更多