在 wxPython 中显示另一个窗口/框架 [英] showing another window/frame in wxPython

查看:37
本文介绍了在 wxPython 中显示另一个窗口/框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对编程、python 和 wxpython 还很陌生.我已经仔细查看了这段代码 HOURS 并尝试在网上到处寻找答案.单击菜单项后,我无法显示新窗口.到目前为止,这是我的代码...

I am fairly new to programming and to python and wxpython. I have looked over this code for literally HOURS and I tried finding an answer everywhere online. I am having trouble getting a new window to show up after a menu item is clicked. Here is my code so far...

import wx

class MainWindow(wx.Frame):

    def __init__(self,parent,id):
        wx.Frame.__init__(self,parent,id,'Python Test App',size=(600,400))
        panel=wx.Panel(self)
        wx.Frame.CenterOnScreen(self)
        ##wx.Frame.Maximize(self)

        status=self.CreateStatusBar()
        menubar=wx.MenuBar()
        file_menu=wx.Menu()
        edit_menu=wx.Menu()

        ID_FILE_NEW = 1
        ID_FILE_OPEN = 2

        ID_EDIT_UNDO = 3
        ID_EDIT_REDO = 4


        file_menu.Append(ID_FILE_NEW,"New Window","This is a new window")
        file_menu.Append(ID_FILE_OPEN,"Open...","This will open a new window")

        edit_menu.Append(ID_EDIT_UNDO,"Undo","This will undo your last action")
        edit_menu.Append(ID_EDIT_REDO,"Redo","This will redo your last undo")


        menubar.Append(file_menu,"File")
        menubar.Append(edit_menu,"Edit")
        self.SetMenuBar(menubar)

        self.Bind(wx.EVT_MENU, NewWindow.new_frame, None, 1)

class NewWindow(wx.Frame):

    def __init__(self,MainWindow,id):
        wx.Frame.__init__(self, None, id, 'New Window', size=(600,400))
        wx.Frame.CenterOnScreen(self)
        self.Show(False)

    def new_frame(self, event):
        NewWindow.Show(True)



if __name__=='__main__':
        app=wx.PySimpleApp()
        frame=MainWindow(parent=None,id=-1)
        frame.Show()
        app.MainLoop()

当我尝试运行此代码时,一旦单击菜单项新窗口",我就会收到此错误消息

When I try to run this code, I get this error message once I click on the menu item "New Window"

TypeError: unbound method new_frame() must be called with NewWindow instance as first argument (got CommandEvent instance instead)

同样,我对编程还很陌生.非常感谢任何帮助,而且,我知道我的代码可能不是最干净"的代码.提前致谢!

Again, I am fairly new to programming. Any help is greatly appreciated and also, I know my code may not be the "cleanest" looking code around. Thanks in advance!

推荐答案

在某种程度上修改你的代码我能够在用户点击一个新窗口选项时显示一个新窗口,

Modifying your code to some extent i was able to show a new window when user clicks a New Window option,

请检查我修改过的内容,让我知道这是否是您想要的??

Do check the stuff that i have modified a let me know if this is what you want??

import wx

class MainWindow(wx.Frame):

    def __init__(self,parent,id):
        wx.Frame.__init__(self,parent,id,'Python Test App',size=(600,400))
        panel=wx.Panel(self)
        wx.Frame.CenterOnScreen(self)

        status=self.CreateStatusBar()
        menubar=wx.MenuBar()
        file_menu=wx.Menu()
        edit_menu=wx.Menu()

        ID_FILE_NEW = 1
        ID_FILE_OPEN = 2

        ID_EDIT_UNDO = 3
        ID_EDIT_REDO = 4


        file_menu.Append(ID_FILE_NEW,"New Window","This is a new window")
        file_menu.Append(ID_FILE_OPEN,"Open...","This will open a new window")

        edit_menu.Append(ID_EDIT_UNDO,"Undo","This will undo your last action")
        edit_menu.Append(ID_EDIT_REDO,"Redo","This will redo your last undo")


        menubar.Append(file_menu,"File")
        menubar.Append(edit_menu,"Edit")
        self.SetMenuBar(menubar)

        self.Bind(wx.EVT_MENU, self.test, None, 1)

    def test(self, event):
        self.new = NewWindow(parent=None, id=-1)
        self.new.Show()

class NewWindow(wx.Frame):

    def __init__(self,parent,id):
        wx.Frame.__init__(self, parent, id, 'New Window', size=(400,300))
        wx.Frame.CenterOnScreen(self)
        #self.new.Show(False)

if __name__=='__main__':
        app=wx.PySimpleApp()
        frame=MainWindow(parent=None,id=-1)
        frame.Show()
        app.MainLoop()

这篇关于在 wxPython 中显示另一个窗口/框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆