鱼C论坛

 找回密码
 立即注册
查看: 1638|回复: 1

wxPython 写的一个记事本出问题

[复制链接]
发表于 2014-10-25 21:57:03 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 miles0918 于 2014-10-26 17:00 编辑

如题,主界面和其他内容正常显示。
但是输入文件路径点击Load Button出问题,提示,如下:
Traceback (most recent call last):
  File "test_panel.py", line 60, in OnOpen
    file_value = path_text.GetValue()
NameError: global name 'path_text' is not defined



                               
登录/注册后可看大图



第60行:file_value = path_text.GetValue()这里有调用错误吗?

  1. import wx

  2. class MyFrame(wx.Frame):
  3.         def __init__(self, title):
  4.                 wx.Frame.__init__(self, parent = None, title = title, size = (640, 380))
  5.                 self.Bind(wx.EVT_CLOSE, self.OnClose)

  6.                 # create menubar and file menu.
  7.                 menuBar = wx.MenuBar()
  8.                 file_menu = wx.Menu()
  9.                 menuBar.Append(file_menu, "file")

  10.                 # create items in the file menu.
  11.                 exit_item = file_menu.Append(wx.ID_EXIT, "exit")
  12.                 self.Bind(wx.EVT_MENU, self.OnClose, exit_item)

  13.                 # help menu.
  14.                 help_menu = wx.Menu()
  15.                 about_item = help_menu.Append(wx.ID_ABOUT, "about")
  16.                 menuBar.Append(help_menu, "help")
  17.                 self.Bind(wx.EVT_MENU, self.OnAbout, about_item)


  18.                 panel = wx.Panel(self)
  19.                 open_button = wx.Button(parent = panel, label = 'open')
  20.                 save_button = wx.Button(parent = panel, label = 'save')
  21.                 path_text = wx.TextCtrl(parent = panel)
  22.                 content_text = wx.TextCtrl(panel, style = wx.TE_MULTILINE)

  23.                 self.Bind(wx.EVT_BUTTON, self.OnOpen, open_button)
  24.                 self.Bind(wx.EVT_BUTTON, self.OnSave, save_button)

  25.                 a_sizer = wx.BoxSizer(wx.HORIZONTAL)
  26.                 a_sizer.Add(path_text, proportion = 1, flag = wx.EXPAND)
  27.                 a_sizer.Add(open_button, proportion = 0, border = 5)
  28.                 a_sizer.Add(save_button, proportion = 0, border = 5)

  29.                 b_sizer = wx.BoxSizer(wx.VERTICAL)
  30.                 b_sizer.Add(a_sizer, proportion = 0, flag = wx.EXPAND, border = 5)
  31.                 b_sizer.Add(content_text, proportion = 1, flag = wx.EXPAND)

  32.                 panel.SetSizer(b_sizer)
  33.                 self.SetMenuBar(menuBar)

  34.         def OnClose(self, evt):
  35.                 dlg = wx.MessageDialog(self, "Do you really want to close this application?",
  36.                         "confirm exit", wx.OK | wx.CANCEL)
  37.                 result = dlg.ShowModal()
  38.                 dlg.Destroy()
  39.                 if result == wx.ID_OK:
  40.                         self.Destroy()

  41.         def OnAbout(self, evt):
  42.                 dlg = wx.MessageDialog(self, "notepad\nVersion:\t1.0")
  43.                 dlg.ShowModal()
  44.                 dlg.Destroy()

  45.         def OnOpen(self, evt):
  46.                 file_value = path_text.GetValue()
  47.                 file_obj = open(file_value)
  48.                 content_text.SetValue(file_obj.read())
  49.                 file_obj.close()

  50.         def OnSave(self, evt):
  51.                 file_value = path_text.GetValue()
  52.                 content = self.content_text.GetValue()
  53.                 file_obj = open(file_value, 'w')
  54.                 file_obj.write(content)
  55.                 file_obj.close()

  56. def main():
  57.         app = wx.App()
  58.         frame = MyFrame('hello shit')
  59.         frame.Show()
  60.         app.MainLoop()

  61. if __name__ == "__main__":
  62.         main()
复制代码



点击open后无法正常打开文件

点击open后无法正常打开文件
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2014-10-26 17:02:56 | 显示全部楼层
求指导啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-2-13 00:15

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表