相互导入2个wx窗口

问题描述 投票:0回答:2

假设我们有 1.py 和 2.py 作为主窗口和子窗口,我们想要创建一个按钮在它们之间导航。 有关该想法的解释请参阅代码: 1.py(主窗口):

import wx

class app(wx.Frame):
    def __init__(self, parent):
        super(app,self).__init__(parent,-1, title= 'example app')
        p = wx.Panel(self,-1)
        self.Center()
        self.btn = wx.Button(p,-1, 'go')
        self.btn.Bind(wx.EVT_BUTTON, self.onBtn)
        self.Show()

    def onBtn(self, event):
        # I need the code to import the 2.py's class.

app = wx.App()
school(None)
app.MainLoop()

2.py(子窗口):

import wx

class appTwo(wx.Frame):
    def __init__(self, parent):
        super(appTwo,self).__init__(parent,-1, title= 'example app')
        p = wx.Panel(self,-1)
        self.Center()
        self.btn = wx.Button(p,-1, 'back')
        self.btn.Bind(wx.EVT_BUTTON, self.onBtn)
        self.Show()

    def onBtn(self, event):
        # I need the code to import the 1.py's class.

app = wx.App()
app2(None)
app.MainLoop()
wxpython
2个回答
0
投票

在此示例中,我定义了两个面板(不是框架),您可以在它们之间切换。按钮事件在框架中处理,而不是在单独的面板中处理

import wx

class MainFrame(wx.Frame):
    def __init__(self, *args, **kwargs):
        super().__init__(None, *args, **kwargs)
        self.Title = 'Wx App'

        self.panel_one = MainPanel(self)
        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(self.panel_one)
        self.SetSizerAndFit(self.sizer)
        self.Center()
        self.Show()

    def onBtn_one(self, event):
        self._clear_sizer()
        self.panel_two = SecondPanel(self)
        self.sizer.Add(self.panel_two)

    def onBtn_two(self, event):
        self._clear_sizer()
        self.panel_one = MainPanel(self)
        self.sizer.Add(self.panel_one)

    def _clear_sizer(self):
        for child in self.sizer.GetChildren():
            if child.IsSizer():
                if child.IsSizer():
                    self._clear_sizer(child.GetSizer())
            else:
                child.GetWindow().Destroy()
        self.sizer.Clear()


class MainPanel(wx.Panel):
    def __init__(self, parent, *args, **kwargs):
        super().__init__(parent, *args, **kwargs)

        txt_info = wx.StaticText(self, label='panel 1')
        btn = wx.Button(self, -1, 'go to 2')
        btn.Bind(wx.EVT_BUTTON, parent.onBtn_one)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(txt_info)
        sizer.Add(btn)
        self.SetSizerAndFit(sizer)


class SecondPanel(wx.Panel):
    def __init__(self, parent, *args, **kwargs):
        super().__init__(parent, *args, **kwargs)

        txt_info = wx.StaticText(self, label='panel 2')
        btn = wx.Button(self,-1, 'go to 1')
        btn.Bind(wx.EVT_BUTTON, parent.onBtn_two)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(txt_info)
        sizer.Add(btn)
        self.SetSizerAndFit(sizer)


if __name__ == '__main__':
    wx_app = wx.App()
    MainFrame()
    wx_app.MainLoop()

0
投票

看看那里。

它构成一个主框架,您可以使用菜单栏上的选项更改面板。 只需使用您需要的面板类更改类,您就可以使用菜单栏根据需要更改窗口。

我的意思是“PlatformDataPanel”和“NewTradingBotPanel”是我正在使用的面板。您可以使用您想要的面板更改这些面板,并且如果您喜欢的解决方案,您可以使用菜单栏在面板之间切换的框架。

否则你可以使用一些按钮,但逻辑是相同的。

import wx, os 
from Resources.Constants import *
from Panels.MainPanel import MainPanel
from Panels.PlatformDataPanel import PlatformDataPanel
from Panels.NewTradingBotPanel import NewTradingBotPanel
from Resources.Strings import Strings

from Utils.RegexUtils import RegexUtils

class MainFrame(wx.Frame):

    __mMainPanel: MainPanel = None
    __mPlatformDataPanel: PlatformDataPanel = None
    __mNewBotPanel: NewTradingBotPanel = None

    __mMenubar: wx.MenuBar = None

    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, title=Strings.STR_TRADING_BOT, size=Constants.DISPLAY_SIZE_MAIN_FRAME)
        wx.Frame.CenterOnScreen(self)
        self.Maximize(True)
        self.Show()
        self.__init_layout()

    def OnCloseMe(self, event):
        self.Close(True)

    def OnCloseWindow(self, event):
        self.Destroy()

#region - Private Methods
    def __init_layout(self):
        self.__init_menubar()
        self.__init_main_panel()
        
#region - Init Menu Methods
    def __init_menubar(self):
        self.__mMenubar = wx.MenuBar()
        self.__init_menu_main_menu()
        self.__init_menu_settings()
        self.__init_menu_views()
        self.__init_menu_new_bot()
        self.SetMenuBar(self.__mMenubar)

    def __init_menu_main_menu(self):
        mainMenu = wx.Menu()
        m00 = mainMenu.Append(-1, Strings.STR_MAIN_MENU)
        self.__mMenubar.Append(mainMenu, Strings.STR_MAIN_MENU)
        self.Bind(wx.EVT_MENU, self.__on_click_menu_main_menu, m00)

    def __init_menu_settings(self):
        settingsMenu = wx.Menu()
        m11 = settingsMenu.Append(-1, Strings.STR_MENU_SETTINGS_PLATFORM_DATA)
        m12 = settingsMenu.Append(-1, Strings.STR_MENU_SETTINGS_BOTS)
        m13 = settingsMenu.Append(-1, Strings.STR_MENU_SETTINGS_SIMULATIONS)
        self.__mMenubar.Append(settingsMenu, Strings.STR_MENU_SETTINGS)
        self.Bind(wx.EVT_MENU, self.__on_click_menu_settings_platform_data, m11)
        self.Bind(wx.EVT_MENU, self.__on_click_menu_settings_bots, m12)
        self.Bind(wx.EVT_MENU, self.__on_click_menu_settings_simulations, m13)

    def __init_menu_views(self):
        viewMenu = wx.Menu()
        m21 = viewMenu.Append(-1, Strings.STR_MENU_VIEW_ASSETS)
        m22 = viewMenu.Append(-1, Strings.STR_MENU_VIEW_BOTS)
        m23 = viewMenu.Append(-1, Strings.STR_MENU_VIEW_SIMULATIONS)
        m24 = viewMenu.Append(-1, Strings.STR_MENU_VIEW_CHARTS)
        self.__mMenubar.Append(viewMenu, Strings.STR_MENU_VIEW)
        self.Bind(wx.EVT_MENU, self.__on_click_menu_view_assets, m21)
        self.Bind(wx.EVT_MENU, self.__on_click_menu_view_bots, m22)
        self.Bind(wx.EVT_MENU, self.__on_click_menu_view_simulations, m23)
        self.Bind(wx.EVT_MENU, self.__on_click_menu_view_charts, m24)

    def __init_menu_new_bot(self):
        newBotMenu = wx.Menu()
        m31 = newBotMenu.Append(-1, Strings.STR_MENU_BOT_NEW_TRADING_BOT)
        m32 = newBotMenu.Append(-1, Strings.STR_MENU_BOT_NEW_DIVIDEND_BOT)
        m33 = newBotMenu.Append(-1, Strings.STR_MENU_BOT_NEW_COPY_TRADER_BOT)
        self.__mMenubar.Append(newBotMenu, Strings.STR_MENU_NEW_BOT)
        self.Bind(wx.EVT_MENU, self.on_click_menu_bot_new_trading_bot, m31)
        self.Bind(wx.EVT_MENU, self.on_click_menu_bot_new_dividend_bot, m32)
        self.Bind(wx.EVT_MENU, self.on_click_menu_bot_new_copy_trader_bot, m33)
#endregion

#region - Init Panels Methods
    def __init_main_panel(self):
        self.__remove_all_panels()
        self.__mMainPanel = MainPanel(self, wx.DisplaySize())
        self.__mMainPanel.Show()

    def __init_platform_data_panel(self):
        self.__remove_all_panels()
        self.__mPlatformDataPanel = PlatformDataPanel(self, wx.DisplaySize())
        self.__mPlatformDataPanel.Show()

    def __init_new_trading_bot_panel(self):
        self.__remove_all_panels()
        self.__mNewBotPanel = NewTradingBotPanel(self, wx.DisplaySize())
        self.__mNewBotPanel.Show()
#endregion

#region - Remove Panel Methods
    def __remove_all_panels(self):
        self.__remove_main_panel()
        self.__remove_platform_data_panel()
        self.__remove_new_trading_bot_panel()

    def __remove_main_panel(self):
        if self.__mMainPanel:
            self.__mMainPanel.Destroy()

    def __remove_platform_data_panel(self):
        if self.__mPlatformDataPanel:
            self.__mPlatformDataPanel.Destroy()

    def __remove_new_trading_bot_panel(self):
        if self.__mNewBotPanel:
            self.__mNewBotPanel.Destroy()
#endregion

#region - On Click Methods
    def __on_click_menu_main_menu(self, evt):
        self.__init_main_panel()

    def __on_click_menu_settings_platform_data(self, evt):
        self.__init_platform_data_panel()

    def __on_click_menu_settings_bots(self, evt):
        logging.info("__on_click_menu_settings_bots")

    def __on_click_menu_settings_simulations(self, evt):
        logging.info("__on_click_menu_settings_simulations")

    def __on_click_menu_view_assets(self, evt):
        logging.info("__on_click_menu_view_assets")

    def __on_click_menu_view_bots(self, evt):
        logging.info("__on_click_menu_view_bots")

    def __on_click_menu_view_simulations(self, evt):
        logging.info("__on_click_menu_view_simulations")

    def __on_click_menu_view_charts(self, evt):
        logging.info("__on_click_menu_view_charts")

    def on_click_menu_bot_new_trading_bot(self, evt):
        self.__init_new_trading_bot_panel()

    def on_click_menu_bot_new_dividend_bot(self, evt):
        logging.info("on_click_menu_bot_new_dividend_bot")

    def on_click_menu_bot_new_copy_trader_bot(self, evt):
        logging.info("on_click_menu_bot_new_copy_trader_bot")
#endregion
#endregion
© www.soinside.com 2019 - 2024. All rights reserved.