尝试做一个文本文件查看器
Trying to do a text file viewer
我正在尝试使用 Python 和 wxWidgets 构建一个极简主义的文本文件编辑器。
这是我第一次构建图形用户界面。
我想构建一个简单的 window,它会在启动时打开并显示文件 1.txt
.
中的内容
单击“下一步”按钮时,编辑器应显示 2.txt
文件的内容。
我制作了以下程序,成功显示了我想要的 window 和小部件,但无法打开文本文件并正确显示它们。
有问题的行已经被注释掉了,我用print()
来显示被打开文件的内容。不仅 print()
显示空字符串,而且似乎没有考虑单击按钮的事件。
这是我的代码:
#!/usr/bin/env python3
import wx
import wx.lib.editor as editor
class Editor(wx.App):
filecounter = 1
def __init__(self):
wx.App.__init__(self, redirect=False)
def OnInit(self):
frame = wx.Frame(
None,
-1,
"blabla",
size=(200, 100),
style=wx.DEFAULT_FRAME_STYLE,
name="wsfacile editor",
)
frame.Show(True)
frame.Bind(wx.EVT_CLOSE, self.OnCloseFrame)
win = self.EditWindow(frame)
if win:
frame.SetSize((800, 600))
win.SetFocus()
self.window = win
frect = frame.GetRect()
else:
frame.Destroy()
return True
self.SetTopWindow(frame)
self.frame = frame
return True
def OnExitApp(self, evt):
self.frame.Close(True)
def OnCloseFrame(self, evt):
evt.Skip()
def GetNextFile(self):
self.filecounter += 1
# self.ed.SetText(self.GetFileText(str(self.filecounter) + ".txt"))
print(self.GetFileText(str(self.filecounter) + ".txt"))
def GetFileText(self, filename):
with open(filename, "r") as myfile:
result = myfile.readlines()
myfile.close()
return result
def EditWindow(self, frame):
win = wx.Panel(frame, -1)
self.ed = editor.Editor(win, -1, style=wx.SUNKEN_BORDER)
next_button = wx.Button(win, 0, "Next")
box = wx.BoxSizer(wx.VERTICAL)
box.Add(self.ed, 1, wx.ALL | wx.GROW, 1)
box.Add(next_button, 0, wx.ALIGN_CENTER, 0)
self.Bind(wx.EVT_BUTTON, self.GetNextFile())
win.SetSizer(box)
win.SetAutoLayout(True)
# self.ed.SetText(self.GetFileText(str(self.filecounter) + ".txt"))
return win
def main():
app = Editor()
app.MainLoop()
if __name__ == "__main__":
main()
此致
行
self.Bind(wx.EVT_BUTTON, self.GetNextFile())
是错误的,它 调用 函数而不是将其设置为处理程序。您应该删除 ()
.
我正在尝试使用 Python 和 wxWidgets 构建一个极简主义的文本文件编辑器。
这是我第一次构建图形用户界面。
我想构建一个简单的 window,它会在启动时打开并显示文件 1.txt
.
单击“下一步”按钮时,编辑器应显示 2.txt
文件的内容。
我制作了以下程序,成功显示了我想要的 window 和小部件,但无法打开文本文件并正确显示它们。
有问题的行已经被注释掉了,我用print()
来显示被打开文件的内容。不仅 print()
显示空字符串,而且似乎没有考虑单击按钮的事件。
这是我的代码:
#!/usr/bin/env python3
import wx
import wx.lib.editor as editor
class Editor(wx.App):
filecounter = 1
def __init__(self):
wx.App.__init__(self, redirect=False)
def OnInit(self):
frame = wx.Frame(
None,
-1,
"blabla",
size=(200, 100),
style=wx.DEFAULT_FRAME_STYLE,
name="wsfacile editor",
)
frame.Show(True)
frame.Bind(wx.EVT_CLOSE, self.OnCloseFrame)
win = self.EditWindow(frame)
if win:
frame.SetSize((800, 600))
win.SetFocus()
self.window = win
frect = frame.GetRect()
else:
frame.Destroy()
return True
self.SetTopWindow(frame)
self.frame = frame
return True
def OnExitApp(self, evt):
self.frame.Close(True)
def OnCloseFrame(self, evt):
evt.Skip()
def GetNextFile(self):
self.filecounter += 1
# self.ed.SetText(self.GetFileText(str(self.filecounter) + ".txt"))
print(self.GetFileText(str(self.filecounter) + ".txt"))
def GetFileText(self, filename):
with open(filename, "r") as myfile:
result = myfile.readlines()
myfile.close()
return result
def EditWindow(self, frame):
win = wx.Panel(frame, -1)
self.ed = editor.Editor(win, -1, style=wx.SUNKEN_BORDER)
next_button = wx.Button(win, 0, "Next")
box = wx.BoxSizer(wx.VERTICAL)
box.Add(self.ed, 1, wx.ALL | wx.GROW, 1)
box.Add(next_button, 0, wx.ALIGN_CENTER, 0)
self.Bind(wx.EVT_BUTTON, self.GetNextFile())
win.SetSizer(box)
win.SetAutoLayout(True)
# self.ed.SetText(self.GetFileText(str(self.filecounter) + ".txt"))
return win
def main():
app = Editor()
app.MainLoop()
if __name__ == "__main__":
main()
此致
行
self.Bind(wx.EVT_BUTTON, self.GetNextFile())
是错误的,它 调用 函数而不是将其设置为处理程序。您应该删除 ()
.