【发布时间】:2019-05-30 18:11:20
【问题描述】:
我目前有一个工作脚本,可让我创建备份文件的副本,获取该副本并将其重命名为 Filename_New,并将原始文件重命名为 Filename_Bad。
我是创建 GUI 和一般 python 代码的新手,但目前有一个 gui,并希望将 3 段特定代码绑定到 gui 中的不同框,以便在步骤 1 中输入文件名时在 gui 它运行我的 python 代码的那部分。
不太确定如何将这两个东西整合在一起,所以任何建议都将不胜感激。在此先感谢,希望下面的代码格式正确。
这是我执行复制过程的一段python代码。
我有两个其他变体,将 _NEW 和 _BAD 添加到其他文件。
我想将此代码绑定到该 GUI 中的文本框中,您可以在其中输入文件名并在您点击 Okay 时执行代码。
### 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
在 gui 中输入文件名,然后在该文件名上执行代码。
【问题讨论】:
标签: python python-2.7 wxpython