使用 WX 显示文件路径
Show file path using WX
我试图在将文件路径放入 NewText 标签后显示它
但它只显示顶部的路径 window...
我该怎么做才能让它发挥作用。
这是我的代码
我正在尝试显示 'Path will be' staticText
所在的路径
import wx
class MyFileDropTarget(wx.FileDropTarget):
def __init__(self, window):
wx.FileDropTarget.__init__(self)
self.window = window
def OnDropFiles(self, x, y, filename):
#self.window.SetInsertionPointEnd()
TempTxt = filename
print(TempTxt)
print(type(TempTxt))
TempTxt = str(TempTxt)
print(type(TempTxt))
self.window.LabelTextUpdate(TempTxt)
return True
class Example(wx.Frame):
def __init__(self, parent, title):
super(Example, self).__init__(parent, title=title)
self.InitUI()
self.Center()
def InitUI(self):
panel = wx.Panel(self)
FileDrTr = MyFileDropTarget(self)
font = wx.SystemSettings.GetFont(wx.SYS_SYSTEM_FONT)
font.SetPointSize(9)
verBox = wx.BoxSizer(wx.VERTICAL)
horBoxOne = wx.BoxSizer(wx.HORIZONTAL)
TextLabel = wx.StaticText(panel, label = 'Drop file hear')
TextLabel.SetFont(font)
horBoxOne.Add(TextLabel, flag=wx.RIGHT, border=10)
DropePlace = wx.TextCtrl(panel)
DropePlace.SetDropTarget(FileDrTr)
horBoxOne.Add(DropePlace, proportion=1)
verBox.Add(horBoxOne, flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.TOP, border=10)
verBox.Add((-1, 10))
horBoxTwo = wx.BoxSizer(wx.HORIZONTAL)
NewText = wx.StaticText(panel, label = 'Path will be')
horBoxTwo.Add(NewText, flag=wx.RIGHT, border=5)
verBox.Add(horBoxTwo, flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.TOP, border=10)
panel.SetSizer(verBox)
def LabelTextUpdate(self, txt):
print(txt)
print(type(txt))
NewText = wx.StaticText(self, label = txt)
def main():
app = wx.App()
ex = Example(None, title = 'drope and see file path')
ex.Show()
app.MainLoop()
if __name__ == '__main__':
main()
在您的 InitUI 中,您创建了一些小部件,其中之一是 NewText,我想它就是您要用于显示文件路径的小部件。您已使用 sizer 将其放置在 window 中。到目前为止一切顺利。
在 LabelTextUpdate 中,您创建了一个全新的小部件并设置了它的文本。该小部件位于您的 window 内,可能位于左上角的坐标处,因此看起来好像您更改了 window 的文本。但实际上你放置了一个新的小部件。
事实上,您想更改 NewText 的标签。您必须在对象中保留对 NewText 的引用,因此在 InitUI 中您将执行(注意自我):
self.NewText = wx.StaticText(panel, label = 'Path will be')
horBoxTwo.Add(self.NewText, flag=wx.RIGHT, border=5)
而LabelTextUpdate将使用这个成员变量:
def LabelTextUpdate(self, txt):
self.NewText.SetLabel(txt)
就是这样。
因为我们正在对 event
文件丢失做出反应,所以将处理分配给事件也是有意义的。
介绍 wx.lib.newevent
:
在这里,我们创建了自己的事件 (EVT_DROP_EVENT) 并为其分配了一个标签 (drop_event)。
drop_event, EVT_DROP_EVENT = wx.lib.newevent.NewEvent()
我们现在有一个事件要bind
,我们可以为它分配一个回调例程,在本例中为LabelTextUpdate
。
self.Bind(EVT_DROP_EVENT, self.LabelTextUpdate)
当文件被删除时,我们设置事件并触发它:
evt = drop_event(data=TempTxt)
wx.PostEvent(self.obj,evt)
回调例程由于绑定到 EVT_DROP_EVENT 而被调用,在此例程中我们处理数据。
希望这很清楚(见下文)。
import wx
import wx.lib.newevent
drop_event, EVT_DROP_EVENT = wx.lib.newevent.NewEvent()
class MyFileDropTarget(wx.FileDropTarget):
def __init__(self, obj):
wx.FileDropTarget.__init__(self)
self.obj = obj
def OnDropFiles(self, x, y, filename):
#filename is a list of 1 or more files
#here we are assuming only 1 file
TempTxt = filename[0]
evt = drop_event(data=TempTxt)
wx.PostEvent(self.obj,evt)
return True
class Example(wx.Frame):
def __init__(self, parent, title):
super(Example, self).__init__(parent, title=title)
self.InitUI()
self.Center()
def InitUI(self):
panel = wx.Panel(self)
FileDrTr = MyFileDropTarget(self)
font = wx.SystemSettings.GetFont(wx.SYS_SYSTEM_FONT)
font.SetPointSize(9)
verBox = wx.BoxSizer(wx.VERTICAL)
horBoxOne = wx.BoxSizer(wx.HORIZONTAL)
TextLabel = wx.StaticText(panel, label = 'Drop file here')
TextLabel.SetFont(font)
horBoxOne.Add(TextLabel, flag=wx.RIGHT, border=10)
DropePlace = wx.TextCtrl(panel)
DropePlace.SetDropTarget(FileDrTr)
#Bind the drop event listener
self.Bind(EVT_DROP_EVENT, self.LabelTextUpdate)
horBoxOne.Add(DropePlace, proportion=1)
verBox.Add(horBoxOne, flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.TOP, border=10)
verBox.Add((-1, 10))
horBoxTwo = wx.BoxSizer(wx.HORIZONTAL)
self.NewText = wx.StaticText(panel, label = 'Path will be')
horBoxTwo.Add(self.NewText, flag=wx.RIGHT, border=5)
verBox.Add(horBoxTwo, flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.TOP, border=10)
panel.SetSizer(verBox)
def LabelTextUpdate(self, event):
txt = event.data
self.NewText.SetLabel(txt)
def main():
app = wx.App()
ex = Example(None, title = 'drop and see file path')
ex.Show()
app.MainLoop()
if __name__ == '__main__':
main()
我试图在将文件路径放入 NewText 标签后显示它 但它只显示顶部的路径 window... 我该怎么做才能让它发挥作用。
这是我的代码 我正在尝试显示 'Path will be' staticText
所在的路径import wx
class MyFileDropTarget(wx.FileDropTarget):
def __init__(self, window):
wx.FileDropTarget.__init__(self)
self.window = window
def OnDropFiles(self, x, y, filename):
#self.window.SetInsertionPointEnd()
TempTxt = filename
print(TempTxt)
print(type(TempTxt))
TempTxt = str(TempTxt)
print(type(TempTxt))
self.window.LabelTextUpdate(TempTxt)
return True
class Example(wx.Frame):
def __init__(self, parent, title):
super(Example, self).__init__(parent, title=title)
self.InitUI()
self.Center()
def InitUI(self):
panel = wx.Panel(self)
FileDrTr = MyFileDropTarget(self)
font = wx.SystemSettings.GetFont(wx.SYS_SYSTEM_FONT)
font.SetPointSize(9)
verBox = wx.BoxSizer(wx.VERTICAL)
horBoxOne = wx.BoxSizer(wx.HORIZONTAL)
TextLabel = wx.StaticText(panel, label = 'Drop file hear')
TextLabel.SetFont(font)
horBoxOne.Add(TextLabel, flag=wx.RIGHT, border=10)
DropePlace = wx.TextCtrl(panel)
DropePlace.SetDropTarget(FileDrTr)
horBoxOne.Add(DropePlace, proportion=1)
verBox.Add(horBoxOne, flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.TOP, border=10)
verBox.Add((-1, 10))
horBoxTwo = wx.BoxSizer(wx.HORIZONTAL)
NewText = wx.StaticText(panel, label = 'Path will be')
horBoxTwo.Add(NewText, flag=wx.RIGHT, border=5)
verBox.Add(horBoxTwo, flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.TOP, border=10)
panel.SetSizer(verBox)
def LabelTextUpdate(self, txt):
print(txt)
print(type(txt))
NewText = wx.StaticText(self, label = txt)
def main():
app = wx.App()
ex = Example(None, title = 'drope and see file path')
ex.Show()
app.MainLoop()
if __name__ == '__main__':
main()
在您的 InitUI 中,您创建了一些小部件,其中之一是 NewText,我想它就是您要用于显示文件路径的小部件。您已使用 sizer 将其放置在 window 中。到目前为止一切顺利。
在 LabelTextUpdate 中,您创建了一个全新的小部件并设置了它的文本。该小部件位于您的 window 内,可能位于左上角的坐标处,因此看起来好像您更改了 window 的文本。但实际上你放置了一个新的小部件。
事实上,您想更改 NewText 的标签。您必须在对象中保留对 NewText 的引用,因此在 InitUI 中您将执行(注意自我):
self.NewText = wx.StaticText(panel, label = 'Path will be')
horBoxTwo.Add(self.NewText, flag=wx.RIGHT, border=5)
而LabelTextUpdate将使用这个成员变量:
def LabelTextUpdate(self, txt):
self.NewText.SetLabel(txt)
就是这样。
因为我们正在对 event
文件丢失做出反应,所以将处理分配给事件也是有意义的。
介绍 wx.lib.newevent
:
在这里,我们创建了自己的事件 (EVT_DROP_EVENT) 并为其分配了一个标签 (drop_event)。
drop_event, EVT_DROP_EVENT = wx.lib.newevent.NewEvent()
我们现在有一个事件要bind
,我们可以为它分配一个回调例程,在本例中为LabelTextUpdate
。
self.Bind(EVT_DROP_EVENT, self.LabelTextUpdate)
当文件被删除时,我们设置事件并触发它:
evt = drop_event(data=TempTxt)
wx.PostEvent(self.obj,evt)
回调例程由于绑定到 EVT_DROP_EVENT 而被调用,在此例程中我们处理数据。
希望这很清楚(见下文)。
import wx
import wx.lib.newevent
drop_event, EVT_DROP_EVENT = wx.lib.newevent.NewEvent()
class MyFileDropTarget(wx.FileDropTarget):
def __init__(self, obj):
wx.FileDropTarget.__init__(self)
self.obj = obj
def OnDropFiles(self, x, y, filename):
#filename is a list of 1 or more files
#here we are assuming only 1 file
TempTxt = filename[0]
evt = drop_event(data=TempTxt)
wx.PostEvent(self.obj,evt)
return True
class Example(wx.Frame):
def __init__(self, parent, title):
super(Example, self).__init__(parent, title=title)
self.InitUI()
self.Center()
def InitUI(self):
panel = wx.Panel(self)
FileDrTr = MyFileDropTarget(self)
font = wx.SystemSettings.GetFont(wx.SYS_SYSTEM_FONT)
font.SetPointSize(9)
verBox = wx.BoxSizer(wx.VERTICAL)
horBoxOne = wx.BoxSizer(wx.HORIZONTAL)
TextLabel = wx.StaticText(panel, label = 'Drop file here')
TextLabel.SetFont(font)
horBoxOne.Add(TextLabel, flag=wx.RIGHT, border=10)
DropePlace = wx.TextCtrl(panel)
DropePlace.SetDropTarget(FileDrTr)
#Bind the drop event listener
self.Bind(EVT_DROP_EVENT, self.LabelTextUpdate)
horBoxOne.Add(DropePlace, proportion=1)
verBox.Add(horBoxOne, flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.TOP, border=10)
verBox.Add((-1, 10))
horBoxTwo = wx.BoxSizer(wx.HORIZONTAL)
self.NewText = wx.StaticText(panel, label = 'Path will be')
horBoxTwo.Add(self.NewText, flag=wx.RIGHT, border=5)
verBox.Add(horBoxTwo, flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.TOP, border=10)
panel.SetSizer(verBox)
def LabelTextUpdate(self, event):
txt = event.data
self.NewText.SetLabel(txt)
def main():
app = wx.App()
ex = Example(None, title = 'drop and see file path')
ex.Show()
app.MainLoop()
if __name__ == '__main__':
main()