关闭wxpython中的第二个窗口

时间:2014-05-12 12:17:01

标签: python window wxpython

我使用以下完整脚本来处理quicktime文件。当文件处理时,我打开第二个窗口,它停留在所有其他窗口的顶部,通知用户“处理文件。请稍候”(代码底部的设置类),直到处理完成,它应该然后关闭第二个窗口并返回主窗口。它运行的那一刻,但是当文件完成处理时,它不是关闭始终在顶部窗口,而是给出了错误:

  File "/Users/user1/Desktop/Python/File_Prrep.py", line 501, in __init__
    self.Close()
  File "/usr/local/lib/wxPython-3.0.0.0/lib/python2.7/site-packages/wx-3.0-osx_cocoa/wx/_core.py", line 9169, in Close
    return _core_.Window_Close(*args, **kwargs)
TypeError: in method 'Window_Close', expected argument 1 of type 'wxWindow *'

我不确定如何修复它。这是我的完整代码:

import wx
import os
import os.path
import inspect
import csv
import subprocess
import sys
import shutil
import re
import urllib2
import threading
import wx.lib.agw.pybusyinfo as PBI
from subprocess import Popen, PIPE

class ScrolledWindow(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, size=(510, 370), style=wx.DEFAULT_FRAME_STYLE & ~ (wx.RESIZE_BORDER | 
                                                wx.RESIZE_BOX | 
                                                wx.MAXIMIZE_BOX))

        self.tabbed = wx.Notebook(self, -1, style=(wx.NB_TOP))

        run_params = {}
        run_params["dropList1"] = ['HD 1920x1080', 'PAL 4x3', 'PAL 16x9', 'NTSC 4x3', 'NTSC 16x9']
        run_params["dropList2"] = ['Progressive', 'Interlaced']
        run_params["running"] = False
        run_params["1stRun"] = True

        self.CreateStatusBar()
        menuBar = wx.MenuBar()
        menu = wx.Menu()
        self.SetMenuBar(menuBar)
        panel = wx.Panel(self, -1)

        self.Centre()
        self.Show()

        self.filePrep = PrepFile(self.tabbed, run_params)
        self.settings = Settings('settings', run_params)

        self.tabbed.AddPage(self.filePrep, "File Prep")

class PrepFile(wx.Panel):
    def __init__(self, parent, run_params):
        wx.Panel.__init__(self, parent)

        self.run_params = run_params
        self.fieldChoice = 'Progressive'
        self.formatOption = 'HD 1920x1080'

        outputOption = '''Format'''
        wx.StaticText(self, -1, outputOption, (33, 22), style=wx.ALIGN_CENTRE)


        self.choice1 = wx.Choice(self, pos=(35, 40), choices=self.run_params["dropList1"])
        self.choice1.SetSelection(0)
        self.choice1.SetFocus()
        self.choice1.Bind(wx.EVT_CHOICE, self.selectOption)

        fieldSetText = '''Fields'''
        wx.StaticText(self, -1, fieldSetText, (33, 82), style=wx.ALIGN_CENTRE)

        self.choice2 = wx.Choice(self, pos=(35, 100), choices=self.run_params["dropList2"])
        self.choice2.SetSelection(0)
        self.choice2.SetFocus()
        self.choice2.Bind(wx.EVT_CHOICE, self.fieldSet)

        self.buttonClose = wx.Button(self, -1, "Quit", pos=(195, 250))
        self.buttonClose.Bind(wx.EVT_BUTTON, self.OnClose)

        greyBox = wx.StaticBox(self, -1, '', pos=(20, 15), size=(235, 130))

        outputtxt3 = '''Drag and Drop Quicktimes'''
        wx.StaticText(self, -1, outputtxt3, pos=(35, 170), style=wx.ALIGN_CENTRE)

        self.drop_target = MyFileDropTarget(self)
        self.SetDropTarget(self.drop_target)
        self.tc_files = wx.TextCtrl(self, wx.ID_ANY, pos=(38, 190), size=(200, 25))
        self.buttonSubmit = wx.Button(self, -1, "Submit", pos=(250,190))
        self.buttonSubmit.Bind(wx.EVT_BUTTON, self.submit)


    def EvtRadioBox(self, event):
        self.mode = (event.GetString())

    def selectOption(self, e):
        self.formatOption = self.choice1.GetStringSelection()

    def fieldSet(self, e):
        self.fieldChoice = self.choice2.GetStringSelection()

    def setSubmissionDrop(self, dropFiles):
        """Called by the FileDropTarget when files are dropped"""
        self.tc_files.SetValue(','.join(dropFiles))
        self.selectedFiles = dropFiles
        print self.selectedFiles

    def submit(self, edit):
        self.run_params["running"] = True
        self.run_params["1stRun"] = False
        Settings(None, self.run_params)
        for item in self.selectedFiles:
            if os.path.isdir(item):
                for root, dirs, files in os.walk(item):
                    for file1 in files:
                        if file1.endswith(".mov"):
                            currentFile = os.path.join(root, file1)
                            self.jesFile(currentFile)
            else:
                if item.endswith(".mov"):
                    self.jesFile(item)
        self.run_params["running"] = False
        Settings(None, self.run_params)

    def OnClose(self, e):
        CloseApp()

    def jesFile(self, currentFile):
        if self.fieldChoice == "Interlaced":
            if self.formatOption == 'HD 1920x1080':
                self.preset = 'HD 1080i'
            elif self.formatOption == 'PAL 4x3':
                self.preset = 'PAL 4x3i'
            elif self.formatOption == 'PAL 16x9':
                self.preset = 'PAL 16x9i'
            elif self.formatOption == 'NTSC 4x3':
                self.preset = 'NTSC 4x3i'
            elif self.formatOption == 'NTSC 16x9':
                self.preset = 'NTSC 16x9i'
        else:
            if self.formatOption == 'HD 1920x1080':
                self.preset = 'HD 1080p'
            elif self.formatOption == 'PAL 4x3':
                self.preset = 'PAL 4x3p'
            elif self.formatOption == 'PAL 16x9':
                self.preset = 'PAL 16x9p'
            elif self.formatOption == 'NTSC 4x3':
                self.preset = 'NTSC 4x3p'
            elif self.formatOption == 'NTSC 16x9':
                self.preset = 'NTSC 16x9p'

        print self.preset
        jesCommand = './JES/JES\ Extensifier.app/Contents/MacOS/JES\ Extensifier   -p   ' + '"' + self.preset + '"' + '   ' + '"' + currentFile + '"'
        print jesCommand
        self.process1 = Popen(jesCommand, shell=True, stdin=PIPE)
        self.assignAudio(currentFile)

    def assignAudio(self, currentFile):
        changeScript = '''
            on run argv
                repeat with a in argv
                    set a's contents to a as POSIX file as alias
                end repeat
                open argv
            end run

            on open aa
                set channel_layouts_map1 to {¬
                    {"Sound Track 1", "Sound Track 1", {"Left"}}, ¬
                    {"Sound Track 2", "Sound Track 2", {"Right"}}, ¬
                    {"Sound Track 3", "Sound Track 3", {"Center"}}, ¬
                    {"Sound Track 4", "Sound Track 4", {"LFE Screen"}}, ¬
                    {"Sound Track 5", "Sound Track 5", {"Left Surround"}}, ¬
                    {"Sound Track 6", "Sound Track 6", {"Right Surround"}}, ¬
                    {"Sound Track 7", "Sound Track 7", {"Left Total"}}, ¬
                    {"Sound Track 8", "Sound Track 8", {"Right Total"}} ¬
                        }
                set channel_layouts_map2 to {¬
                    {"Sound Track 1", "Sound Track 1", {"Left"}}, ¬
                    {"Sound Track 2", "Sound Track 2", {"Right"}}, ¬
                    {"Sound Track 3", "Sound Track 3", {"Center"}}, ¬
                    {"Sound Track 4", "Sound Track 4", {"LFE Screen"}}, ¬
                    {"Sound Track 5", "Sound Track 5", {"Left Surround"}}, ¬
                    {"Sound Track 6", "Sound Track 6", {"Right Surround"}}, ¬
                    {"Sound Track 7", "Sound Track 7", {"Left Total", "Right Total"}} ¬
                        }
                set channel_layouts_map3 to {¬
                    {"Sound Track", "Sound Track", {"Left", "Right"}} ¬
                        }
                set channel_layouts_map4 to {¬
                    {"Sound Track 1", "Sound Track 1", {"Left"}}, ¬
                    {"Sound Track 2", "Sound Track 2", {"Right"}} ¬
                        }

                repeat with a in aa
                    set f to a's POSIX path
                    set k to count_sound_tracks(f, {_close:false})
                    if k = 8 then
                        remap_audio_channels(f, channel_layouts_map1)
                    else if k = 7 then
                        remap_audio_channels(f, channel_layouts_map2)
                    else if k = 1 then
                        remap_audio_channels(f, channel_layouts_map3)
                    else if k = 2 then
                        remap_audio_channels(f, channel_layouts_map4)
                    else
                        -- ignore it (just close it)
                        close_document(f, {_save:false})
                    end if
                end repeat
            end open

            on count_sound_tracks(f, {_close:_close})
                tell application id "com.apple.quicktimeplayer" -- QuickTime Player 7 Pro
                    open (f as POSIX file)
                    tell (document 1 whose path = f)
                        repeat until exists
                            delay 0.2
                        end repeat
                        set k to count (tracks whose audio channel count > 0)
                        if _close then close
                    end tell
                end tell
                return k
            end count_sound_tracks

            on close_document(f, {_save:_save})
                tell application id "com.apple.quicktimeplayer" -- QuickTime Player 7 Pro
                    tell (document 1 whose path = f)
                        if exists then
                            if _save and modified then save
                            close
                        end if
                    end tell
                end tell
            end close_document

            on remap_audio_channels(f, channel_layouts_map)
                script o
                    property map : channel_layouts_map
                    property pp : {}
                    property qq : {}

                    -- get name and id of sound tracks
                    tell application id "com.apple.quicktimeplayer" -- QuickTime Player 7 Pro
                        activate
                        open (f as POSIX file)
                        tell (document 1 whose path = f)
                            repeat until exists
                                delay 0.2
                            end repeat
                            tell (tracks whose audio channel count > 0)
                                set {pp, qq} to {name, id} -- name and id of sound tracks
                            end tell
                        end tell
                    end tell

                    -- remap audio channel layouts as specified
                    tell application "System Events"
                        tell (process 1 whose bundle identifier = "com.apple.quicktimeplayer")
                            -- open movie properties window
                            keystroke "j" using {command down}

                            tell (window 1 whose subrole = "AXDialog") -- properties for movie
                                repeat until exists
                                    delay 0.2
                                end repeat
                                repeat with m in my map
                                    set {trk, undef, layouts} to m
                                    -- [TRK:
                                    repeat 1 times
                                        if trk's class = integer then
                                            if trk < 1 or trk > (count my pp) then exit repeat -- TRK:
                                            set trk to my pp's item trk
                                        end if
                                        tell scroll area 1
                                            tell table 1
                                                tell (row 1 whose text field 1's value = trk) -- target sound track whose name = trk
                                                    if not (exists) then exit repeat -- TRK:
                                                    select
                                                end tell
                                            end tell
                                        end tell
                                        tell tab group 1
                                            click radio button 3 -- audio settings
                                            tell scroll area 1
                                                tell table 1 -- channel assignment table
                                                    set ix to count layouts
                                                    repeat with i from 1 to count rows
                                                        if i > ix then exit repeat
                                                        tell row i -- channel i
                                                            tell pop up button 1
                                                                click
                                                                tell menu 1 -- channel assignment menu
                                                                    tell (menu item 1 whose title = layouts's item i)
                                                                        if exists then click
                                                                    end tell
                                                                end tell
                                                            end tell
                                                        end tell
                                                    end repeat
                                                end tell
                                            end tell
                                        end tell
                                    end repeat
                                    -- /TRK:]
                                end repeat

                                -- close movie properties window
                                click (button 1 whose subrole = "AXCloseButton")
                            end tell
                        end tell
                    end tell

                    -- rename sound tracks as specified
                    tell application id "com.apple.quicktimeplayer"
                        set scale of document 1 to normal
                        tell document 1
                            repeat with m in my map
                            end repeat
                            if modified then save
                            close
                        end tell
                    end tell

                end script
#                tell o to run
                run script o
            end remap_audio_channels

            on _index_of(xx, x) -- renamed _bsearch() v0.1
                script o
                    property aa : xx
                    local i, j, k
                    if {x} is not in my aa then return 0
                    set i to 1
                    set j to count my aa
                    repeat while j > i
                        set k to (i + j) div 2
                        if {x} is in my aa's items i thru k then
                            set j to k
                        else
                            set i to k + 1
                        end if
                    end repeat
                    return i
                end script
                tell o to run
            end _index_of'''

        p = Popen(['osascript', '-'] + [currentFile], stdin=PIPE, stdout=PIPE, stderr=PIPE)
        stdout, stderr = p.communicate(changeScript)
        print (p.returncode, stdout, stderr)
        print "Done"

class MyFileDropTarget(wx.FileDropTarget):
    """"""
    def __init__(self, window):
        wx.FileDropTarget.__init__(self)
        self.window = window

    def OnDropFiles(self, x, y, filenames):
        self.window.setSubmissionDrop(filenames)

class CloseApp(wx.Frame):
    def __init__(e):
        sys.exit(0)

class Settings(wx.Frame):
    def __init__(self, parent, run_params):
        self.run_params = run_params
        if self.run_params["running"] == True:
            wx.Frame.__init__(self, parent, -1, 'Please Wait', size=(350,150), pos=(35, 100), style=wx.STAY_ON_TOP | wx.DEFAULT_FRAME_STYLE)
            wx.StaticText(self, -1, "Processing files. Please wait", style=wx.ALIGN_CENTRE)
            self.Centre()
            self.Show()
        else:
            if self.run_params["1stRun"] != True:
                self.Close()

app = wx.App()
ScrolledWindow(None, -1, 'iTunes Quicktime File Prep')
app.MainLoop()

1 个答案:

答案 0 :(得分:1)

您正尝试关闭构造函数中的设置窗口。你不能这样做,因为那时窗口根本不存在。

将您的设置类修改为而不是执行此操作:

class Settings(wx.Frame):
    def __init__(self, parent, run_params):
        self.run_params = run_params
        if self.run_params["running"] == True:
            wx.Frame.__init__(self, parent, -1, 'Please Wait', size=(350,150), pos=(35, 100), style=wx.STAY_ON_TOP | wx.DEFAULT_FRAME_STYLE)
            wx.StaticText(self, -1, "Processing files. Please wait", style=wx.ALIGN_CENTRE)
            self.Centre()
            self.Show()

    def OnClose(self):
        if self.run_params["running"] == False:
            self.Close()