如何将python合并到wxpython中

发布于 2024-11-17 01:56:28 字数 3480 浏览 4 评论 0原文

你好,我是一名业余程序员。我构建了一个简单的文本扭曲游戏并将其命名为 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 中我将其称为类还是函数?

Hello I am an amateur programmer. I built a simple text twister game and named it as texttwister_compy.py. I also have been able to build a simple GUI. But I need to learn how to integrate my python program into my wxpython GUI.
Here is my wxpython code:

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()

I also have a question regarding the demos in wxpython question, how exactly are you to open them.

My final question is how do you do that?

So here is the program for the 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

How do I integrate this with the code above? So my main loop would be like a class or a function like say class Loop. Then in the main wxpython do I call it as a class or a function?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

她比我温柔 2024-11-24 01:56:28

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

尽管此时您的游戏可能需要采用 OOP 形式,因为程序性不太适合这种情况。

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

Simply bind your action events to your button or mouse clicks. So if your game involves changing the number in a box and incrementing it one for every click a simple call to your function in the game to as input and update the GUI. On that click its not only supposed to update the box, but do something in your game that can have consequences or rewards, which will then reflect back onto your GUI (ie. updating your score).

Though by this point your game will likely need to be in OOP form, as procedural will not likely work well for this.

Testing the test cases, you first need to find the examples. Often they are located in your python-directory/wxpython/examples for most modules. You'll find them, and launch them like you would any other python script. Often there will be a readme, and the wxpython docs also tell you what to do.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文