【问题标题】:Adding executable code to my wxpython gui text boxes将可执行代码添加到我的 wxpython gui 文本框
【发布时间】: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


    【解决方案1】:

    欢迎来到 StackOverflow。

    对您的问题的简短回答是,您需要将 GUI 中的按钮Bind 使用某种方法。您可以在下面的代码中看到这是如何完成的。

    我稍微更改了您的代码,因为您定义了 wx.BoxSixer,但随后您将小部件添加到 wx.GridBagSizer。此外,我更改了按钮以向您展示如何搜索带有wx.FileDialog 的文件,以防您不想输入文件的路径并且因为我假设(可能不正确)关闭按钮将关闭应用程序。无需这样做,只需单击 X 即可关闭应用程序。

    使用 cmets 编写代码

    ### 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
    

    【讨论】:

    • 感谢您的帮助,您的 cmets 帮助这更有意义,但是当我运行脚本时,我得到一个 (TypeError: unbound method main() must be called with Example instance as first争论(一无所获))
    • @Skabrewz 我错过了您使用的是 python2.7。我更新了代码的最后一部分。它现在应该可以工作了。
    • 是的,这是 2.7,但是当我运行这个版本时,我得到 (PyAssertionError: C++ assertion "rowspan > 0" failed at C:\BUILD\wxPython-src-3.0.2.0\include\wx/ wxGBSpan::SetRowspan() 中的 gbsizer.h(81):行跨度应严格为正)
    • @Skabrewz 我正在创建一个虚拟环境来测试代码。您使用的是哪个版本的 wxPython?哪个python 2.7?
    • Python 2.7.14 和 3.0.2.0 msw(经典),我现在受限于 ArcGIS 10.6,并且大部分时间都坚持使用 python 2.7,直到我的办公室为我们获得了一些 ArcPro 许可并且我可以玩Python 3+ 和 wxpython 4+
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-04
    • 2011-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多