对如何构建 GUI (wxpython) 感到困惑

发布于 2024-11-09 22:19:27 字数 856 浏览 4 评论 0原文

我从一本书转到另一本书,从谷歌搜索到另一本书,我注意到每一本书都以完全不同的方式启动主窗口。

我不想养成坏习惯,所以有人可以给我最好的这些选择以及为什么这是更好的方法。以下是我见过的所有方法

A)
类 iFrame(wx.Frame): def init(....): wx.Frame._init_(...)

B)
类 iFrame(wx.Frame): def init(...): super_init_(...)

C)
然后我看到一些使用面板来代替,例如
类 iPanel(wx.Panel) def init(...): wx.Panel.init(...)

D)
更令人困惑的是,有些人正在使用 wx 的常规 App 类
iApp类(wx.App): def OnInit(self): wx.Frame.init(...)

如果我的某些结构是错误的,请原谅我,但我突然想起这些,再次提问...其中哪一个,如果ANY 是构建 GUI 的最佳方式。当教程和书籍都以不同的方式编辑时,很难遵循它们

:抱歉,如果格式不正确,但通常它可以工作......

I've gone from one book to another, one google search to another and I notice EVERY SINGLE ONE starts the main window in a completely different way.

I don't want to pick up bad habits so can someone please give me the best of these options and why its the better method. Below are all the ways i've seen it done

A)
class iFrame(wx.Frame):
def init(....):
wx.Frame._init_(...)

B)
class iFrame(wx.Frame):
def init(...):
super_init_(...)

C)
Then I see some that uses the Panel instead such as
class iPanel(wx.Panel)
def init(...):
wx.Panel.init(...)

D)
And even more confusing some are using the regular App class of wx
class iApp(wx.App):
def OnInit(self):

wx.Frame.init(...)

Forgive me if some of my structures are wrong but I'm recalling these off the top of my head, Question again...Which one of these, IF ANY is the best way to structure the GUI. It's hard following tutorials and books when they all do things in diff ways

edit: Sorry if format is not correct, but normally it works...

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

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

发布评论

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

评论(4

不念旧人 2024-11-16 22:19:27

我最喜欢的开始 wx 应用程序开发的方法是:

import wx

class MainWindow(wx.Frame):
    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs)

        self.panel = wx.Panel(self)
        self.button = wx.Button(self.panel, label="Test")

        self.sizer = wx.BoxSizer()
        self.sizer.Add(self.button)

        self.panel.SetSizerAndFit(self.sizer)  
        self.Show()

app = wx.App(False)
win = MainWindow(None)
app.MainLoop()

另请参阅这个 问题,这是相关的。

My favorite way to start wx application development is:

import wx

class MainWindow(wx.Frame):
    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs)

        self.panel = wx.Panel(self)
        self.button = wx.Button(self.panel, label="Test")

        self.sizer = wx.BoxSizer()
        self.sizer.Add(self.button)

        self.panel.SetSizerAndFit(self.sizer)  
        self.Show()

app = wx.App(False)
win = MainWindow(None)
app.MainLoop()

See also this question, which is related.

孤城病女 2024-11-16 22:19:27

别担心。现在做出错误的选择不会毁掉你未来的编程。

您提到的选项都没有错误。它们的做法都不同,因为不同的应用程序有不同的要求。没有一种方法是最好的。

只要做你想做的事,做对你有用的事,一旦你更加熟悉,你就会明白为什么不同的代码会有不同的做法。

Don't worry about it. You aren't going to ruin your future programming by making the wrong choice now.

None of the options you mention are wrong. They all do things differently because different applications have different requirements. No one way is best.

Just work on what you want and do what works for you, and once you have greater familiarity then you'll understand why different code does it differently.

掩饰不了的爱 2024-11-16 22:19:27

我经历了惨痛的教训才知道,正如在每个应用程序中一样,封装很重要。 wxPython 特有的一点是,主框架对象应该只有一个面板小部件,以及可选的菜单栏、工具栏和状态栏小部件。没有别的了。

这是我的新 wxPython 应用程序的基本模式:

(2019 年 2 月 7 日更新:Wx Phoenix 和 Python 3)

import wx


class MainFrame(wx.Frame):
    """Create MainFrame class."""
    def __init__(self, *args, **kwargs):
        super(MainFrame, self).__init__(None, *args, **kwargs)
        self.Title = 'Basic wxPython module'
        self.SetMenuBar(MenuBar(self))
        self.ToolBar = MainToolbar(self)
        self.status_bar = StatusBar(self).status_bar
        self.Bind(wx.EVT_CLOSE, self.on_quit_click)
        panel = MainPanel(self)
        sizer = wx.BoxSizer()
        sizer.Add(panel)
        self.SetSizerAndFit(sizer)
        self.Centre()
        self.Show()

    def on_quit_click(self, event):
        """Handle close event."""
        del event
        wx.CallAfter(self.Destroy)


class MainPanel(wx.Panel):
    """Panel class to contain frame widgets."""
    def __init__(self, parent, *args, **kwargs):
        super(MainPanel, self).__init__(parent, *args, **kwargs)

        """Create and populate main sizer."""
        sizer = wx.BoxSizer(wx.VERTICAL)
        cmd_quit = wx.Button(self, id=wx.ID_EXIT)
        cmd_quit.Bind(wx.EVT_BUTTON, parent.on_quit_click)
        sizer.Add(cmd_quit)
        self.SetSizer(sizer)


class MenuBar(wx.MenuBar):
    """Create the menu bar."""
    def __init__(self, parent, *args, **kwargs):
        super(MenuBar, self).__init__(*args, **kwargs)
        # File menu
        file_menu = wx.Menu()
        self.Append(file_menu, '&File')

        quit_menu_item = wx.MenuItem(file_menu, wx.ID_EXIT)
        parent.Bind(wx.EVT_MENU, parent.on_quit_click, id=wx.ID_EXIT)

        file_menu.Append(quit_menu_item)


class MainToolbar(wx.ToolBar):
    """Create tool bar."""
    def __init__(self, parent, *args, **kwargs):
        super(MainToolbar, self).__init__(parent, *args, **kwargs)

        #quit_bmp =  wx.ArtProvider.GetBitmap(wx.ART_QUIT)
        #self.AddTool(wx.ID_EXIT, 'Quit', wx.Bitmap(quit_bmp))
        #self.SetToolShortHelp(wx.ID_EXIT, 'Quit')
        #self.Bind(wx.EVT_TOOL, parent.on_quit_click, id=wx.ID_EXIT)
        #self.Realize()


class StatusBar(object):
    def __init__(self, parent):
        """Create status bar."""
        self.status_bar = parent.CreateStatusBar()


if __name__ == '__main__':
    """Run the application."""
    screen_app = wx.App()
    main_frame = MainFrame()
    screen_app.MainLoop()

I have learned the hard way that, as in every application, encapsulation is important. And peculiar to wxPython, the main frame object should have precisely one panel widget, plus optional menu bar, toolbar and status bar widgets. Nothing else.

This is my basic pattern for new wxPython applications:

(Updated 2019 Feb 07: Wx Phoenix and Python 3)

import wx


class MainFrame(wx.Frame):
    """Create MainFrame class."""
    def __init__(self, *args, **kwargs):
        super(MainFrame, self).__init__(None, *args, **kwargs)
        self.Title = 'Basic wxPython module'
        self.SetMenuBar(MenuBar(self))
        self.ToolBar = MainToolbar(self)
        self.status_bar = StatusBar(self).status_bar
        self.Bind(wx.EVT_CLOSE, self.on_quit_click)
        panel = MainPanel(self)
        sizer = wx.BoxSizer()
        sizer.Add(panel)
        self.SetSizerAndFit(sizer)
        self.Centre()
        self.Show()

    def on_quit_click(self, event):
        """Handle close event."""
        del event
        wx.CallAfter(self.Destroy)


class MainPanel(wx.Panel):
    """Panel class to contain frame widgets."""
    def __init__(self, parent, *args, **kwargs):
        super(MainPanel, self).__init__(parent, *args, **kwargs)

        """Create and populate main sizer."""
        sizer = wx.BoxSizer(wx.VERTICAL)
        cmd_quit = wx.Button(self, id=wx.ID_EXIT)
        cmd_quit.Bind(wx.EVT_BUTTON, parent.on_quit_click)
        sizer.Add(cmd_quit)
        self.SetSizer(sizer)


class MenuBar(wx.MenuBar):
    """Create the menu bar."""
    def __init__(self, parent, *args, **kwargs):
        super(MenuBar, self).__init__(*args, **kwargs)
        # File menu
        file_menu = wx.Menu()
        self.Append(file_menu, '&File')

        quit_menu_item = wx.MenuItem(file_menu, wx.ID_EXIT)
        parent.Bind(wx.EVT_MENU, parent.on_quit_click, id=wx.ID_EXIT)

        file_menu.Append(quit_menu_item)


class MainToolbar(wx.ToolBar):
    """Create tool bar."""
    def __init__(self, parent, *args, **kwargs):
        super(MainToolbar, self).__init__(parent, *args, **kwargs)

        #quit_bmp =  wx.ArtProvider.GetBitmap(wx.ART_QUIT)
        #self.AddTool(wx.ID_EXIT, 'Quit', wx.Bitmap(quit_bmp))
        #self.SetToolShortHelp(wx.ID_EXIT, 'Quit')
        #self.Bind(wx.EVT_TOOL, parent.on_quit_click, id=wx.ID_EXIT)
        #self.Realize()


class StatusBar(object):
    def __init__(self, parent):
        """Create status bar."""
        self.status_bar = parent.CreateStatusBar()


if __name__ == '__main__':
    """Run the application."""
    screen_app = wx.App()
    main_frame = MainFrame()
    screen_app.MainLoop()
栩栩如生 2024-11-16 22:19:27

为了回应 XilyummY 的评论,我添加了这个附加答案,以展示如何在单独的文件中组织主要类。

这是我基于四个文件的解决方案:

  1. main.py:应用程序的主框架和应用程序加载器;
  2. main_panel.py:应用程序的主面板;
  3. menu_bar.py:框架的菜单栏定义;
  4. tool_bar.py:框架中的工具栏。

代码按以下顺序排列:

main.py

import wx

from main_panel import MainPanel
from menu_bar import MenuBar
from tool_bar import MainToolbar


class MainFrame(wx.Frame):
    """Create MainFrame class."""
    def __init__(self, *args, **kwargs):
        super(MainFrame, self).__init__(None, *args, **kwargs)
        self.Title = 'Basic wxPython module'
        self.SetMenuBar(MenuBar(self))
        self.ToolBar = MainToolbar(self)
        self.status_bar = StatusBar(self).status_bar
        self.Bind(wx.EVT_CLOSE, self.on_quit_click)
        panel = MainPanel(self)
        sizer = wx.BoxSizer()
        sizer.Add(panel)
        self.SetSizerAndFit(sizer)
        self.Centre()
        self.Show()

    def on_quit_click(self, event):
        """Handle close event."""
        del event
        wx.CallAfter(self.Destroy)


class StatusBar(object):
    def __init__(self, parent):
        """Create status bar."""
        self.status_bar = parent.CreateStatusBar()


if __name__ == '__main__':
    """Run the application."""
    screen_app = wx.App()
    main_frame = MainFrame()
    screen_app.MainLoop()

main_panel.py

import wx


class MainPanel(wx.Panel):
    """Panel class to contain frame widgets."""
    def __init__(self, parent, *args, **kwargs):
        super(MainPanel, self).__init__(parent, *args, **kwargs)

        """Create and populate main sizer."""
        sizer = wx.BoxSizer(wx.VERTICAL)
        cmd_quit = wx.Button(self, id=wx.ID_EXIT)
        cmd_quit.Bind(wx.EVT_BUTTON, parent.on_quit_click)
        sizer.Add(cmd_quit)
        self.SetSizer(sizer)

menu_bar.py

import wx


class MenuBar(wx.MenuBar):
    """Create the menu bar."""
    def __init__(self, parent, *args, **kwargs):
        super(MenuBar, self).__init__(*args, **kwargs)
        # File menu
        file_menu = wx.Menu()
        self.Append(file_menu, '&File')

        quit_menu_item = wx.MenuItem(file_menu, wx.ID_EXIT)
        parent.Bind(wx.EVT_MENU, parent.on_quit_click, id=wx.ID_EXIT)

        file_menu.Append(quit_menu_item)

tool_bar.py

import wx


class MainToolbar(wx.ToolBar):
    """Create tool bar."""
    def __init__(self, parent, *args, **kwargs):
        super(MainToolbar, self).__init__(parent, *args, **kwargs)

        new_bmp =  wx.ArtProvider.GetBitmap(wx.ART_NEW)
        #preferences_bmp = wx.Bitmap('images/preferences.png')
        quit_bmp =  wx.ArtProvider.GetBitmap(wx.ART_QUIT)

        self.AddTool(wx.ID_NEW, 'New', new_bmp)
        #self.AddTool(wx.ID_PREFERENCES, 'Preferences', preferences_bmp)
        self.AddTool(wx.ID_EXIT, 'Quit', quit_bmp)

        self.SetToolShortHelp(wx.ID_NEW, 'New ...')
        self.SetToolShortHelp(wx.ID_PREFERENCES, 'Preferences ...')
        self.SetToolShortHelp(wx.ID_EXIT, 'Quit')

        self.Bind(wx.EVT_TOOL, parent.on_quit_click, id=wx.ID_EXIT)

        self.Realize()

In response to a comment by XilyummY, I have added this additional answer to show how the main classes can be organised in separate files.

This is my solution based on four files:

  1. main.py: the main frame for the application and the application loader;
  2. main_panel.py: the main panel for the application;
  3. menu_bar.py: the menu bar definition for the frame;
  4. tool_bar.py: the tool bar from the frame.

The code follows in this order:

main.py

import wx

from main_panel import MainPanel
from menu_bar import MenuBar
from tool_bar import MainToolbar


class MainFrame(wx.Frame):
    """Create MainFrame class."""
    def __init__(self, *args, **kwargs):
        super(MainFrame, self).__init__(None, *args, **kwargs)
        self.Title = 'Basic wxPython module'
        self.SetMenuBar(MenuBar(self))
        self.ToolBar = MainToolbar(self)
        self.status_bar = StatusBar(self).status_bar
        self.Bind(wx.EVT_CLOSE, self.on_quit_click)
        panel = MainPanel(self)
        sizer = wx.BoxSizer()
        sizer.Add(panel)
        self.SetSizerAndFit(sizer)
        self.Centre()
        self.Show()

    def on_quit_click(self, event):
        """Handle close event."""
        del event
        wx.CallAfter(self.Destroy)


class StatusBar(object):
    def __init__(self, parent):
        """Create status bar."""
        self.status_bar = parent.CreateStatusBar()


if __name__ == '__main__':
    """Run the application."""
    screen_app = wx.App()
    main_frame = MainFrame()
    screen_app.MainLoop()

main_panel.py

import wx


class MainPanel(wx.Panel):
    """Panel class to contain frame widgets."""
    def __init__(self, parent, *args, **kwargs):
        super(MainPanel, self).__init__(parent, *args, **kwargs)

        """Create and populate main sizer."""
        sizer = wx.BoxSizer(wx.VERTICAL)
        cmd_quit = wx.Button(self, id=wx.ID_EXIT)
        cmd_quit.Bind(wx.EVT_BUTTON, parent.on_quit_click)
        sizer.Add(cmd_quit)
        self.SetSizer(sizer)

menu_bar.py

import wx


class MenuBar(wx.MenuBar):
    """Create the menu bar."""
    def __init__(self, parent, *args, **kwargs):
        super(MenuBar, self).__init__(*args, **kwargs)
        # File menu
        file_menu = wx.Menu()
        self.Append(file_menu, '&File')

        quit_menu_item = wx.MenuItem(file_menu, wx.ID_EXIT)
        parent.Bind(wx.EVT_MENU, parent.on_quit_click, id=wx.ID_EXIT)

        file_menu.Append(quit_menu_item)

tool_bar.py

import wx


class MainToolbar(wx.ToolBar):
    """Create tool bar."""
    def __init__(self, parent, *args, **kwargs):
        super(MainToolbar, self).__init__(parent, *args, **kwargs)

        new_bmp =  wx.ArtProvider.GetBitmap(wx.ART_NEW)
        #preferences_bmp = wx.Bitmap('images/preferences.png')
        quit_bmp =  wx.ArtProvider.GetBitmap(wx.ART_QUIT)

        self.AddTool(wx.ID_NEW, 'New', new_bmp)
        #self.AddTool(wx.ID_PREFERENCES, 'Preferences', preferences_bmp)
        self.AddTool(wx.ID_EXIT, 'Quit', quit_bmp)

        self.SetToolShortHelp(wx.ID_NEW, 'New ...')
        self.SetToolShortHelp(wx.ID_PREFERENCES, 'Preferences ...')
        self.SetToolShortHelp(wx.ID_EXIT, 'Quit')

        self.Bind(wx.EVT_TOOL, parent.on_quit_click, id=wx.ID_EXIT)

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