当我按下“运行goCTF”时,如何将参数保存到文件中?

时间:2019-08-08 15:00:46

标签: python wxpython

当在文本框(parameter1和parameter2)中写入内容时,我想将数字或参数保存到我按下“ run goCTF”按钮创建的文件(new.txt)中。

我尝试了os.system("echo %s, parameter >> new.txt"),但实际上并没有用。我真的很需要这里的帮助。

import os
import wx

card_goctf = "goctf"     
card_mrc = "mrc files (*.mrc)"   


########################################################################
class MyForm(wx.Frame):

    #----------------------------------------------------------------------
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY,
                          "Welcome to goCTF",pos = (10,10), size = (500,500))
        panel = wx.Panel(self, wx.ID_ANY)
        self.goctf_path = self.currentDirectory = os.getcwd()
        self.mic_path = self.goctf_path

        # create the buttons and bindings
        openFileDlgBtn = wx.Button(panel, label="step 1: locate goCTF prgram")
        openFileDlgBtn.Bind(wx.EVT_BUTTON, self.onOpenFile)

        dirDlgBtn = wx.Button(panel, label="step 2: locat microgaphs directory")
        dirDlgBtn.Bind(wx.EVT_BUTTON, self.onDir)

        runBtn = wx.Button(panel, label="step 3: run goCTF")
        runBtn.Bind(wx.EVT_BUTTON, self.onRun)


##################################

        sizer = wx.GridBagSizer(10, 10)

        line = wx.StaticLine(panel)
        sizer.Add(line, pos=(4, 0), span=(1, 20), flag=wx.EXPAND|wx.BOTTOM, border=10)

        text2 = wx.StaticText(panel, label="Parameter 1")
        sizer.Add(text2, pos=(5, 0), flag=wx.LEFT, border=10)

        tc1 = wx.TextCtrl(panel)
        tc1.Bind(wx.EVT_TEXT,self.OnKeyTyped)
        #tc1 = wx.TextCtrl(panel,style=wx.TE_PROCESS_ENTER)
        #tc1.Bind(wx.EVT_TEXT_ENTER,self.OnKeyTyped)
        sizer.Add(tc1, pos=(5, 1), span=(1, 3), flag=wx.TOP)

        text3 = wx.StaticText(panel, label="Parameter 2")
        sizer.Add(text3, pos=(6, 0), flag=wx.LEFT, border=10)

        tc2 = wx.TextCtrl(panel)
        sizer.Add(tc2, pos=(6, 1), span=(1, 3), flag=wx.TOP)



########################################

        # put the buttons in a sizer
        #sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(openFileDlgBtn, pos=(1, 1), span=(1, 3), flag=wx.TOP)
        sizer.Add(dirDlgBtn, pos=(2, 1), span=(1, 3), flag=wx.TOP)
        sizer.Add(runBtn, pos=(3, 1), span=(1, 3), flag=wx.TOP)

        panel.SetSizer(sizer)

    #----------------------------------------------------------------------
    def onDir(self, event):
        """
        Show the DirDialog and print the user's choice to stdout
        """
        dlg = wx.DirDialog(self, "Choose a directory:",
                           style=wx.DD_DEFAULT_STYLE
                           #| wx.DD_DIR_MUST_EXIST
                           #| wx.DD_CHANGE_DIR
                           )
        if dlg.ShowModal() == wx.ID_OK:
            self.mic_path = dlg.GetPath()
            print("micrographs direcotry: %s" % self.mic_path)
        dlg.Destroy()

    #----------------------------------------------------------------------
    def onOpenFile(self, event):
        """
        Create and show the Open FileDialog
        """
        dlg = wx.FileDialog(
            self, message="Choose a file",
            defaultDir=self.currentDirectory, 
            defaultFile="",
            wildcard=card_goctf,
            style=wx.FD_OPEN | wx.FD_MULTIPLE | wx.FD_CHANGE_DIR
            )
        if dlg.ShowModal() == wx.ID_OK:
            self.goctf_path = dlg.GetPath()
            print(self.goctf_path)
        dlg.Destroy()


     #----------------------------------------------------------------------

    def OnKeyTyped(self, event): 
      print(event.GetString())




    def onRun(self, event):
        """
        Run goCTF program
        """ 
        os.chdir(self.mic_path)
        os.system(str(self.goctf_path))
        print(self.goctf_path)
        os.system("touch new.txt")
        os.system("echo %s, parameter >> new.txt")




# Run the program
if __name__ == "__main__":
    app = wx.App(False)
    frame = MyForm()
    frame.Show()
    app.MainLoop()

1 个答案:

答案 0 :(得分:0)

您尝试过类似的方法吗?

import wx
import json

class YourClass:

    def onRun(self, event):
        """
        Run goCTF program
        """ 
        path = "C:\\test"
        data = {'goctf_path': path}

        #use this for write:
        self.saveFileJson(data, 'data.txt')
        #use this for read:
        self.readFileJson('data.txt')                   

    def saveFileJson(self, data, path):
        with open(path, 'w') as outfile:
            json.dump(data, outfile)
            print("File saved in %s" % path)

    def readFileJson(self, path):
        with open('data.txt') as json_file:
            data = json.load(json_file)
            print('Readed data: ' + data['goctf_path']) 

希望这可以为您提供帮助。好看。