将可执行代码添加到我的 wxpython gui 文本框
Adding executable code to my wxpython gui text boxes
我目前有一个工作脚本,可以让我创建备份文件的副本,获取该副本并将其重命名为 Filename_New,并将原始文件重命名为 Filename_Bad。
我对创建 GUI 和一般 python 代码不熟悉,但目前有一个 gui,并且想将 3 个特定代码段绑定到 gui 中的不同框,以便当您在 gui 中输入步骤 1 中的文件名,它运行我的 python 代码的那部分。
不太确定如何将这两件事整合在一起,所以任何建议将不胜感激。在此先感谢,希望下面的代码格式正确。
这是我的一段 python 代码,用于执行复制过程。
我有两个上述过程的其他变体,它们将 _NEW 和 _BAD 添加到其他文件。
我想将此代码与文本框中的 GUI 相关联,您可以在文本框中输入文件名,然后在您点击“确定”时执行代码。
### Do all your imports as needed
import wx, wx.lib.newevent
import os, sys, copy, shutil
class Example(wx.Frame):
def __init__(self, parent, title):
super(Example, self).__init__(parent, title=title)
self.InitUI()
self.Centre()
def InitUI(self):
panel = wx.Panel(self)
font = wx.SystemSettings.GetFont(wx.SYS_SYSTEM_FONT)
font.SetPointSize(9)
#### As already mentioned you defined a wx.BoxSizer but later were using
#### a wx.GridBagSizer. By the way I also changed a little bit the span
#### and flags of the widgets when added to the wx.GridBagSizer
sizer = wx.GridBagSizer(1, 1)
text = wx.StaticText(panel, label="Enter the VR File That Crashed: ")
sizer.Add(text, pos=(0, 0), span=(1, 2), flag=wx.EXPAND|wx.TOP|wx.LEFT|wx.BOTTOM, border=5)
#### tc will be used by other methods so it is better to use self.tc
self.tc = wx.TextCtrl(panel)
sizer.Add(self.tc, pos=(1, 0), span=(1, 2),
flag=wx.EXPAND|wx.LEFT|wx.RIGHT, border=5)
#### Changed the label of the buttons
buttonOk = wx.Button(panel, label="Search File", size=(90, 28))
buttonClose = wx.Button(panel, label="Do Stuffs", size=(90, 28))
sizer.Add(buttonOk, pos=(2, 0), flag=wx.ALIGN_CENTER|wx.RIGHT|wx.BOTTOM, border=10)
sizer.Add(buttonClose, pos=(2, 1), flag=wx.ALIGN_CENTER|wx.RIGHT|wx.BOTTOM, border=10)
panel.SetSizer(sizer)
#### This is how you Bind the button to a method so everytime the button
#### is clicked the method is executed
buttonOk.Bind(wx.EVT_BUTTON, self.SearchFile)
buttonClose.Bind(wx.EVT_BUTTON, self.DoStuffs)
def SearchFile(self, event):
#### This is how you use the wx.FileDialog and put the selected path in
#### the wx.TextCtrl
dlg = wx.FileDialog(None, message="Select File", style=wx.FD_OPEN|wx.FD_CHANGE_DIR|wx.FD_FILE_MUST_EXIST|wx.FD_PREVIEW)
if dlg.ShowModal() == wx.ID_OK:
self.tc.SetValue(dlg.GetPath())
else:
pass
def DoStuffs(self, event):
#### This is how you get the path to the selected/typed file and then
#### do your stuffs
def copy_vrb(oldvr):
newvrb = os.path.splitext(oldvr)[0] + "_COPY"
shutil.copy(oldvr, newvrb + ".vrb")
def file_rename(oldvr):
newvrb = os.path.splitext(oldvr)[0] + "_BAD"
shutil.copy(oldvr, newvrb + ".vr")
def rename_copy(oldvr):
newvrb = os.path.splitext(oldvr)[0] + "_NEW"
shutil.copy(oldvr, newvrb + ".vr")
oldvrb = self.tc.GetValue()
copy_vrb(oldvr)
file_rename(oldvr)
rename_copy(oldvr)
print(oldvr)
if __name__ == '__main__':
app = wx.App()
ex = Example(None, title='Rename')
ex.Show()
app.MainLoop()
else:
pass
在图形用户界面中输入一个文件名,然后让代码在该文件名上执行。
欢迎使用 Whosebug。
对您问题的简短回答是,您需要 Bind
将 GUI 中的按钮设置为某种方法。您可以在下面的代码中看到这是如何完成的。
我稍微更改了您的代码,因为您定义了一个 wx.BoxSixer
,但后来又将小部件添加到 wx.GridBagSizer
。此外,我更改了按钮以向您展示如何搜索带有 wx.FileDialog
的文件,以防万一您不想键入文件的路径并且因为我假设(可能不正确)关闭按钮是关闭应用程序。不需要这个,只需单击 X 即可关闭应用程序。
带注释的代码
### Do all your imports as needed
import wx, wx.lib.newevent
import os, sys, copy, shutil
class Example(wx.Frame):
def __init__(self, parent, title):
super(Example, self).__init__(parent, title=title)
self.InitUI()
self.Centre()
def InitUI(self):
panel = wx.Panel(self)
font = wx.SystemSettings.GetFont(wx.SYS_SYSTEM_FONT)
font.SetPointSize(9)
#### As already mentioned you defined a wx.BoxSizer but later were using
#### a wx.GridBagSizer. By the way I also changed a little bit the span
#### and flags of the widgets when added to the wx.GridBagSizer
sizer = wx.GridBagSizer(1, 1)
text = wx.StaticText(panel, label="Enter the VR File That Crashed: ")
sizer.Add(text, pos=(0, 0), span=(0, 2), flag=wx.EXPAND|wx.TOP|wx.LEFT|wx.BOTTOM, border=5)
#### tc will be used by other methods so it is better to use self.tc
self.tc = wx.TextCtrl(panel)
sizer.Add(self.tc, pos=(1, 0), span=(0, 2),
flag=wx.EXPAND|wx.LEFT|wx.RIGHT, border=5)
#### Changed the label of the buttons
buttonOk = wx.Button(panel, label="Search File", size=(90, 28))
buttonClose = wx.Button(panel, label="Do Stuffs", size=(90, 28))
sizer.Add(buttonOk, pos=(2, 0), flag=wx.ALIGN_CENTER|wx.RIGHT|wx.BOTTOM, border=10)
sizer.Add(buttonClose, pos=(2, 1), flag=wx.ALIGN_CENTER|wx.RIGHT|wx.BOTTOM, border=10)
panel.SetSizer(sizer)
#### This is how you Bind the button to a method so everytime the button
#### is clicked the method is executed
buttonOk.Bind(wx.EVT_BUTTON, self.SearchFile)
buttonClose.Bind(wx.EVT_BUTTON, self.DoStuffs)
def SearchFile(self, event):
#### This is how you use the wx.FileDialog and put the selected path in
#### the wx.TextCtrl
dlg = wx.FileDialog(None, message="Select File", style=wx.FD_OPEN|wx.FD_CHANGE_DIR|wx.FD_FILE_MUST_EXIST|wx.FD_PREVIEW)
if dlg.ShowModal() == wx.ID_OK:
self.tc.SetValue(dlg.GetPath())
else:
pass
def DoStuffs(self, event):
#### This is how you get the path to the selected/typed file and then
#### do your stuffs
oldvrb = self.tc.GetValue()
print(oldvrb)
if __name__ == '__main__':
app = wx.App()
ex = Example(None, title='Rename')
ex.Show()
app.MainLoop()
else:
pass
我目前有一个工作脚本,可以让我创建备份文件的副本,获取该副本并将其重命名为 Filename_New,并将原始文件重命名为 Filename_Bad。
我对创建 GUI 和一般 python 代码不熟悉,但目前有一个 gui,并且想将 3 个特定代码段绑定到 gui 中的不同框,以便当您在 gui 中输入步骤 1 中的文件名,它运行我的 python 代码的那部分。
不太确定如何将这两件事整合在一起,所以任何建议将不胜感激。在此先感谢,希望下面的代码格式正确。
这是我的一段 python 代码,用于执行复制过程。
我有两个上述过程的其他变体,它们将 _NEW 和 _BAD 添加到其他文件。
我想将此代码与文本框中的 GUI 相关联,您可以在文本框中输入文件名,然后在您点击“确定”时执行代码。
### Do all your imports as needed
import wx, wx.lib.newevent
import os, sys, copy, shutil
class Example(wx.Frame):
def __init__(self, parent, title):
super(Example, self).__init__(parent, title=title)
self.InitUI()
self.Centre()
def InitUI(self):
panel = wx.Panel(self)
font = wx.SystemSettings.GetFont(wx.SYS_SYSTEM_FONT)
font.SetPointSize(9)
#### As already mentioned you defined a wx.BoxSizer but later were using
#### a wx.GridBagSizer. By the way I also changed a little bit the span
#### and flags of the widgets when added to the wx.GridBagSizer
sizer = wx.GridBagSizer(1, 1)
text = wx.StaticText(panel, label="Enter the VR File That Crashed: ")
sizer.Add(text, pos=(0, 0), span=(1, 2), flag=wx.EXPAND|wx.TOP|wx.LEFT|wx.BOTTOM, border=5)
#### tc will be used by other methods so it is better to use self.tc
self.tc = wx.TextCtrl(panel)
sizer.Add(self.tc, pos=(1, 0), span=(1, 2),
flag=wx.EXPAND|wx.LEFT|wx.RIGHT, border=5)
#### Changed the label of the buttons
buttonOk = wx.Button(panel, label="Search File", size=(90, 28))
buttonClose = wx.Button(panel, label="Do Stuffs", size=(90, 28))
sizer.Add(buttonOk, pos=(2, 0), flag=wx.ALIGN_CENTER|wx.RIGHT|wx.BOTTOM, border=10)
sizer.Add(buttonClose, pos=(2, 1), flag=wx.ALIGN_CENTER|wx.RIGHT|wx.BOTTOM, border=10)
panel.SetSizer(sizer)
#### This is how you Bind the button to a method so everytime the button
#### is clicked the method is executed
buttonOk.Bind(wx.EVT_BUTTON, self.SearchFile)
buttonClose.Bind(wx.EVT_BUTTON, self.DoStuffs)
def SearchFile(self, event):
#### This is how you use the wx.FileDialog and put the selected path in
#### the wx.TextCtrl
dlg = wx.FileDialog(None, message="Select File", style=wx.FD_OPEN|wx.FD_CHANGE_DIR|wx.FD_FILE_MUST_EXIST|wx.FD_PREVIEW)
if dlg.ShowModal() == wx.ID_OK:
self.tc.SetValue(dlg.GetPath())
else:
pass
def DoStuffs(self, event):
#### This is how you get the path to the selected/typed file and then
#### do your stuffs
def copy_vrb(oldvr):
newvrb = os.path.splitext(oldvr)[0] + "_COPY"
shutil.copy(oldvr, newvrb + ".vrb")
def file_rename(oldvr):
newvrb = os.path.splitext(oldvr)[0] + "_BAD"
shutil.copy(oldvr, newvrb + ".vr")
def rename_copy(oldvr):
newvrb = os.path.splitext(oldvr)[0] + "_NEW"
shutil.copy(oldvr, newvrb + ".vr")
oldvrb = self.tc.GetValue()
copy_vrb(oldvr)
file_rename(oldvr)
rename_copy(oldvr)
print(oldvr)
if __name__ == '__main__':
app = wx.App()
ex = Example(None, title='Rename')
ex.Show()
app.MainLoop()
else:
pass
在图形用户界面中输入一个文件名,然后让代码在该文件名上执行。
欢迎使用 Whosebug。
对您问题的简短回答是,您需要 Bind
将 GUI 中的按钮设置为某种方法。您可以在下面的代码中看到这是如何完成的。
我稍微更改了您的代码,因为您定义了一个 wx.BoxSixer
,但后来又将小部件添加到 wx.GridBagSizer
。此外,我更改了按钮以向您展示如何搜索带有 wx.FileDialog
的文件,以防万一您不想键入文件的路径并且因为我假设(可能不正确)关闭按钮是关闭应用程序。不需要这个,只需单击 X 即可关闭应用程序。
带注释的代码
### Do all your imports as needed
import wx, wx.lib.newevent
import os, sys, copy, shutil
class Example(wx.Frame):
def __init__(self, parent, title):
super(Example, self).__init__(parent, title=title)
self.InitUI()
self.Centre()
def InitUI(self):
panel = wx.Panel(self)
font = wx.SystemSettings.GetFont(wx.SYS_SYSTEM_FONT)
font.SetPointSize(9)
#### As already mentioned you defined a wx.BoxSizer but later were using
#### a wx.GridBagSizer. By the way I also changed a little bit the span
#### and flags of the widgets when added to the wx.GridBagSizer
sizer = wx.GridBagSizer(1, 1)
text = wx.StaticText(panel, label="Enter the VR File That Crashed: ")
sizer.Add(text, pos=(0, 0), span=(0, 2), flag=wx.EXPAND|wx.TOP|wx.LEFT|wx.BOTTOM, border=5)
#### tc will be used by other methods so it is better to use self.tc
self.tc = wx.TextCtrl(panel)
sizer.Add(self.tc, pos=(1, 0), span=(0, 2),
flag=wx.EXPAND|wx.LEFT|wx.RIGHT, border=5)
#### Changed the label of the buttons
buttonOk = wx.Button(panel, label="Search File", size=(90, 28))
buttonClose = wx.Button(panel, label="Do Stuffs", size=(90, 28))
sizer.Add(buttonOk, pos=(2, 0), flag=wx.ALIGN_CENTER|wx.RIGHT|wx.BOTTOM, border=10)
sizer.Add(buttonClose, pos=(2, 1), flag=wx.ALIGN_CENTER|wx.RIGHT|wx.BOTTOM, border=10)
panel.SetSizer(sizer)
#### This is how you Bind the button to a method so everytime the button
#### is clicked the method is executed
buttonOk.Bind(wx.EVT_BUTTON, self.SearchFile)
buttonClose.Bind(wx.EVT_BUTTON, self.DoStuffs)
def SearchFile(self, event):
#### This is how you use the wx.FileDialog and put the selected path in
#### the wx.TextCtrl
dlg = wx.FileDialog(None, message="Select File", style=wx.FD_OPEN|wx.FD_CHANGE_DIR|wx.FD_FILE_MUST_EXIST|wx.FD_PREVIEW)
if dlg.ShowModal() == wx.ID_OK:
self.tc.SetValue(dlg.GetPath())
else:
pass
def DoStuffs(self, event):
#### This is how you get the path to the selected/typed file and then
#### do your stuffs
oldvrb = self.tc.GetValue()
print(oldvrb)
if __name__ == '__main__':
app = wx.App()
ex = Example(None, title='Rename')
ex.Show()
app.MainLoop()
else:
pass