单击工具栏中的按钮时无法动态更改文本(wxpython)

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

我对 wxPython 库不是很有经验,目前正在开发一个使用工具栏和事件处理的个人投资组合应用程序。但是,当我单击工具栏中的按钮时,文本并没有像我预期的那样动态变化。我不确定我可能犯了什么错误。目前我只包含了个人信息部分的事件处理方法。如果有人有使用 wxPython 创建应用程序的经验,我将非常感谢你帮助解决这个问题。

我当前的代码:

import wx


class MyPortfolio(wx.Frame):
    def __init__(self):
        super().__init__(None, title="Krishnan\'s Portfolio", size=(800, 600))

        self.panel = wx.Panel(self)

        self.create_toolbar()
        self.create_menu()

        self.Bind(wx.EVT_TOOL, self.on_toolbar_click)
        self.Bind(wx.EVT_MENU, self.on_menu_click)

        self.Show()

    def create_toolbar(self):
        toolbar = self.CreateToolBar()
        toolbar.AddTool(1, 'Personal Info', wx.Bitmap('icons/personal.png'))
        toolbar.AddTool(2, 'Education', wx.Bitmap('icons/education.png'))
        toolbar.AddTool(3, 'Work Experience', wx.Bitmap('icons/work.png'))
        toolbar.AddTool(4, 'Skills', wx.Bitmap('icons/skills.png'))
        toolbar.AddTool(5, 'Projects', wx.Bitmap('icons/projects.png'))
        toolbar.Realize()

    def create_menu(self):
        menubar = wx.MenuBar()
        file_menu = wx.Menu()
        file_menu.Append(wx.ID_OPEN, '&Open')
        file_menu.Append(wx.ID_SAVE, '&Save')
        file_menu.Append(wx.ID_EXIT, '&Quit')
        menubar.Append(file_menu, '&File')

        edit_menu = wx.Menu()
        edit_menu.Append(wx.ID_ANY, 'Cut')
        edit_menu.Append(wx.ID_ANY, 'Copy')
        edit_menu.Append(wx.ID_ANY, 'Paste')
        menubar.Append(edit_menu, '&Edit')

        self.SetMenuBar(menubar)

    def show_personal_info(self):
        self.panel.DestroyChildren()

        personal_info_panel = wx.Panel(self.panel, size=self.panel.GetSize())
        personal_info_panel.SetBackgroundColour(wx.Colour(230, 230, 230))

        name_label = wx.StaticText(
            personal_info_panel, label="Krishnan S")
        name_label.SetFont(wx.Font(16, wx.FONTFAMILY_DEFAULT,
                                   wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD))

        email_label = wx.StaticText(
            personal_info_panel, label="Email: [email protected]")
        email_label.SetFont(wx.Font(12, wx.FONTFAMILY_DEFAULT,
                            wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL))

        phone_label = wx.StaticText(
            personal_info_panel, label="Phone: +91-9999999999")
        phone_label.SetFont(wx.Font(12, wx.FONTFAMILY_DEFAULT,
                            wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL))

        image = wx.Image('images/KrishnanS.jpg', wx.BITMAP_TYPE_ANY)
        image_resized = image.Scale(200, 200, wx.IMAGE_QUALITY_HIGH)
        profile_pic_bitmap = wx.Bitmap(image_resized)

        profile_pic = wx.StaticBitmap(
            personal_info_panel, bitmap=profile_pic_bitmap)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(name_label, 0, wx.ALL, 10)
        sizer.Add(email_label, 0, wx.ALL, 10)
        sizer.Add(phone_label, 0, wx.ALL, 10)
        sizer.Add(profile_pic, 0, wx.ALL, 10)

        personal_info_panel.SetSizer(sizer)
        personal_info_panel.Layout()

    def show_education(self):
        # Code to display the education section
        pass

    def show_work_experience(self):
        # Code to display the work experience section
        pass

    def show_skills(self):
        # Code to display the skills section
        pass

    def show_projects(self):
        # Code to display the projects section
        pass

    def open_file(self):
        # Code to open a file
        pass

    def save_file(self):
        # Code to save a file
        pass

    def on_toolbar_click(self, event):
        toolbar_item_id = event.GetId()
        if toolbar_item_id == 1:
            self.show_personal_info()
        elif toolbar_item_id == 2:
            self.show_education()
        elif toolbar_item_id == 3:
            self.show_work_experience()
        elif toolbar_item_id == 4:
            self.show_skills()
        elif toolbar_item_id == 5:
            self.show_projects()

    def on_menu_click(self, event):
        menu_item_id = event.GetId()
        if menu_item_id == wx.ID_OPEN:
            self.open_file()
        elif menu_item_id == wx.ID_SAVE:
            self.save_file()
        elif menu_item_id == wx.ID_EXIT:
            self.Close()

    def open_file(self):
        with wx.FileDialog(self, "Open file", wildcard="Text files (*.txt)|*.txt",
                           style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST) as fileDialog:

            if fileDialog.ShowModal() == wx.ID_CANCEL:
                return

            # Proceed loading the file chosen by the user
            pathname = fileDialog.GetPath()
            try:
                with open(pathname, 'r') as file:
                    self.editor.SetValue(file.read())
            except IOError:
                wx.LogError("Cannot open file '%s'." % pathname)

    def save_file(self):
        with wx.FileDialog(self, "Save file", wildcard="Text files (*.txt)|*.txt",
                           style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT) as fileDialog:

            if fileDialog.ShowModal() == wx.ID_CANCEL:
                return

            # Proceed saving the file chosen by the user
            pathname = fileDialog.GetPath()
            try:
                with open(pathname, 'w') as file:
                    file.write(self.editor.GetValue())
            except IOError:
                wx.LogError("Cannot save file '%s'." % pathname)


app = wx.App()
MyPortfolio()
app.MainLoop()

python event-handling wxpython toolbar
1个回答
0
投票

你没有得到工具栏事件,因为你绑定了自己。

将工具栏更改为

self.toolbar
并绑定到它,即

(请原谅位图)

    self.toolbar.Bind(wx.EVT_TOOL, self.on_toolbar_click)
    self.Bind(wx.EVT_MENU, self.on_menu_click)

    self.Show()

def create_toolbar(self):
    self.toolbar = self.CreateToolBar()
    self.toolbar.AddTool(1, 'Personal Info', wx.Bitmap('stop.png'))
    self.toolbar.AddTool(2, 'Education', wx.Bitmap('stop.png'))
    self.toolbar.AddTool(3, 'Work Experience', wx.Bitmap('stop.png'))
    self.toolbar.AddTool(4, 'Skills', wx.Bitmap('stop.png'))
    self.toolbar.AddTool(5, 'Projects', wx.Bitmap('stop.png'))
    self.toolbar.Realize() 
© www.soinside.com 2019 - 2024. All rights reserved.