将可执行代码添加到我的wxpython gui文本框中

时间:2019-05-30 18:11:20

标签: python python-2.7 wxpython

我目前有一个工作脚本,该脚本可让我创建备份文件的副本,将其复制并重命名为Filename_New,然后将原始文件重命名为Filename_Bad。

我是创建GUI和python代码的新手,但是目前具有gui,并且想将3个特定的代码段绑定到gui中的不同框中,以便在步骤1中输入文件名时在gui中,它将运行我的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

在gui中输入文件名,并在该文件名上执行代码。

1 个答案:

答案 0 :(得分:1)

欢迎使用StackOverflow。

这个问题的简短答案是,您需要将GUI中的按钮Bind设为某种方法。您可以在下面的代码中看到如何完成此操作。

我稍稍更改了代码,因为您正在定义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