我对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: xyz@gmail.com")
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()
1条答案
按热度按时间1l5u6lss1#
您不会获得工具栏事件,因为您正在绑定到self。
将工具栏更改为
self.toolbar
并绑定到该工具栏,即(请原谅位图)