如何预防节目()使gui不响应

2024-09-27 00:13:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在开发一个应用程序,当你将数据文件拖放到gui上时,它会绘制数据文件。我的问题是节目()导致禁用gui,直到关闭绘图。你知道吗

为了避免这种情况,我尝试使用plt.ion()(即交互模式)和plt.show(block=False)。你知道吗

然而,这两种方法只能部分解决问题,因为它会导致一些不必要的副作用,例如调整绘图的大小实际上并没有调整它的大小,您只会在扩展窗口空间中得到屏幕上的内容。另外,单击绘图上的x(退出)按钮也不会关闭它。你知道吗

下面是最少量的代码来展示这一点:

import wx
import re
import tempfile
import numpy as np
import matplotlib.pyplot as plt

class PlottingManager():

    def plotData(self,filename, xcol, ycol):
        [tx,ty] = self.getData(filename,xcol,ycol)
        plt.plot(tx, ty)

    def getData(self,filename, xcol, ycol):
        print "Plotting : ", str(filename)
        fileIn = open(filename,'r')
        fileStr = fileIn.read()
        csvType = False
        if ',' in fileStr:
            csvType = True
        if csvType:

            data = np.genfromtxt(filename, unpack=True, delimiter=',')
        else:
            fileStr = re.sub('\s+', ' ', fileStr).strip()
            fp = tempfile.NamedTemporaryFile()
            fp.write(fileStr)
            data = np.genfromtxt(filename, unpack=True)

        tx = data[xcol]
        ty = data[ycol]
        return [tx,ty]

    def showPlot(self): #Function that calls plt.show()
        plt.grid(True);
        plt.legend();
        plt.ion()
        plt.show()

MENU_FILE_EXIT = wx.NewId()
DRAG_SOURCE    = wx.NewId()

class PlotFileDropTarget(wx.TextDropTarget):
    def __init__(self, PlottingManager, obj):
        wx.TextDropTarget.__init__(self)
        self.obj = obj
        self.plotManager = PlottingManager

    def OnDropText(self, x, y, data):
        self.obj.WriteText("Will plot | "+ data[7:-2] + '\n\n')
        self.plotManager.plotData(data[7:-2],0,1)
        self.plotManager.showPlot()

class MainWindow(wx.Frame):
    def __init__(self,parent,id,title):
        wx.Frame.__init__(self,parent, wx.ID_ANY, title, size = (750,600), style=wx.DEFAULT_FRAME_STYLE|wx.NO_FULL_REPAINT_ON_RESIZE)
        self.SetBackgroundColour(wx.WHITE)
        # Setup plotting manager
        self.plotManager = PlottingManager()
        # setup dragdrop box
        self.text = wx.TextCtrl(self, DRAG_SOURCE, "", pos=(0,0), size=(750,200), style = wx.TE_MULTILINE|wx.HSCROLL)
        self.dt1 = PlotFileDropTarget(self.plotManager,self.text)
        self.text.SetDropTarget(self.dt1)

        wx.EVT_RIGHT_DOWN(self.text, self.OnDragInit)
        self.Show()

    def CloseWindow(self, event):
        self.Close()

    def OnDragInit(self, event):
        tdo = wx.PyTextDataObject(self.text.GetStringSelection())
        tds = wx.DropSource(self.text)
        tds.SetData(tdo)
        tds.DoDragDrop(True)

class DropPlot(wx.App):
    def OnInit(self):
        frame = MainWindow(None, -1, "Drag data to plot")
        self.SetTopWindow(frame)
        return True

# main loop
app = DropPlot(0)
app.MainLoop()

以下是数据文件:

-0.0000000000 0.0000000000
-0.0108492172 0.1583380438
-0.0216984343 0.3166760876
-0.0325476515 0.4750141313
-0.0433968686 0.6333521751
-0.0542460858 0.7916902189
-0.0650953029 0.9500282627
-0.0759445201 1.1083663065
-0.0867937372 1.2667043503
-0.0976429544 1.4250423940
-0.1084921715 1.5833804378
-0.1193413887 1.7417184816
-0.1301906058 1.9000565254
-0.1410398230 2.0583945692
-0.1518890401 2.2167326130
-0.1627382573 2.3750706567
-0.1735874745 2.5334087005
-0.1844366916 2.6917467443
-0.1952859088 2.8500847881
-0.2061351259 3.0084228319
-0.2169843431 3.1667608756
-0.2278335602 3.3250989194
-0.2386827774 3.4834369632
-0.2495319945 3.6417750070
-0.2603812117 3.8001130508
-0.2712304288 3.9584510946
-0.2820796460 4.1167891383
-0.2929288631 4.2751271821
-0.3037780803 4.4334652259
-0.3146272974 4.5918032697
-0.3254765146 4.7501413135
-0.3363257317 4.9084793573
-0.3471749489 5.0668174010
-0.3580241661 5.2251554448
-0.3688733832 5.3834934886
-0.3797226004 5.5418315324
-0.3905718175 5.7001695762
-0.4014210347 5.8585076199
-0.4122702518 6.0168456637
-0.4231194690 6.1751837075
-0.4339686861 6.3335217513
-0.4448179033 6.4918597951
-0.4556671204 6.6501978389
-0.4665163376 6.8085358826
-0.4773655547 6.9668739264
-0.4882147719 7.1252119702
-0.4990639890 7.2835500140
-0.5099132062 7.4418880578
-0.5207624234 7.6002261016
-0.5316116405 7.7585641453
-0.5424608577 7.9169021891
-0.5533100748 8.0752402329
-0.5641592920 8.2335782767
-0.5750085091 8.3919163205
-0.5858577263 8.5502543642
-0.5967069434 8.7085924080
-0.6075561606 8.8669304518
-0.6184053777 9.0252684956
-0.6292545949 9.1836065394
-0.6401038120 9.3419445832
-0.6509530292 9.5002826269
-0.6618022463 9.6586206707
-0.6726514635 9.8169587145
-0.6835006806 9.9752967583
-0.6943498978 10.1336348021
-0.7051991150 10.2919728459
-0.7160483321 10.4503108896
-0.7268975493 10.6086489334
-0.7377467664 10.7669869772
-0.7485959836 10.9253250210
-0.7594452007 11.0836630648
-0.7702944179 11.2420011085
-0.7811436350 11.4003391523
-0.7919928522 11.5586771961
-0.8028420693 11.7170152399
-0.8136912865 11.8753532837
-0.8245405036 12.0336913275
-0.8353897208 12.1920293712
-0.8462389379 12.3503674150
-0.8570881551 12.5087054588
-0.8679373723 12.6670435026
-0.8787865894 12.8253815464
-0.8896358066 12.9837195901
-0.9004850237 13.1420576339
-0.9113342409 13.3003956777
-0.9221834580 13.4587337215
-0.9330326752 13.6170717653
-0.9438818923 13.7754098091
-0.9547311095 13.9337478528
-0.9655803266 14.0920858966
-0.9764295438 14.2504239404
-0.9872787609 14.4087619842
-0.9981279781 14.5671000280
-1.0089771952 14.7254380718
-1.0198264124 14.8837761155
-1.0306756296 15.0421141593
-1.0415248467 15.2004522031
-1.0523740639 15.3587902469
-1.0632232810 15.5171282907
-1.0740724982 15.6754663344
-1.0849217153 15.8338043782
-1.0957709325 15.9921424220
-1.1234471716 16.1497056125
-1.1511234108 16.3072688030
-1.1787996500 16.4648319936
-1.2064758892 16.6223951841
-1.2341521284 16.7799583746
-1.2618283675 16.9375215651
-1.2895046067 17.0950847556
-1.3171808459 17.2526479461
-1.3448570851 17.4102111367
-1.3725333243 17.5677743272
-1.4002095634 17.7253375177
-1.4278858026 17.8829007082
-1.4555620418 18.0404638987
-1.4832382810 18.1980270892
-1.5109145201 18.3555902798
-1.5385907593 18.5131534703
-1.5662669985 18.6707166608
-1.5939432377 18.8282798513
-1.6216194769 18.9858430418
-1.6492957160 19.1434062323
-1.6769719552 19.3009694229
-1.7046481944 19.4585326134
-1.7323244336 19.6160958039
-1.7600006728 19.7736589944
-1.7876769119 19.9312221849
-1.8153531511 20.0887853754
-1.8430293903 20.2463485660
-1.8707056295 20.4039117565
-1.8983818686 20.5614749470
-1.9260581078 20.7190381375
-1.9537343470 20.8766013280
-1.9814105862 21.0341645185
-2.0090868254 21.1917277090
-2.0367630645 21.3492908996
-2.0644393037 21.5068540901
-2.0921155429 21.6644172806
-2.1197917821 21.8219804711
-2.1474680213 21.9795436616
-2.1751442604 22.1371068521
-2.2028204996 22.2946700427
-2.2304967388 22.4522332332
-2.2581729780 22.6097964237
-2.2858492171 22.7673596142
-2.3135254563 22.9249228047
-2.3412016955 23.0824859952
-2.3688779347 23.2400491858
-2.3965541739 23.3976123763
-2.4242304130 23.5551755668
-2.4519066522 23.7127387573
-2.4795828914 23.8703019478
-2.5072591306 24.0278651383
-2.5349353698 24.1854283289
-2.5626125527 24.3429915194
-2.6327781321 24.4799792649
-2.7029437115 24.6169670104
-2.7731092909 24.7539547558
-2.8432748702 24.8909425013
-2.9134404496 25.0279302468
-2.9836060290 25.1649179923
-3.0537716084 25.3019057378
-3.1239371877 25.4388934833
-3.1941027671 25.5758812288
-3.2642683465 25.7128689743
-3.3344339259 25.8498567198
-3.4045995052 25.9868444653
-3.4747650846 26.1238322108
-3.5449306640 26.2608199563
-3.6150962434 26.3978077017
-3.6852618227 26.5347954472
-3.7554274021 26.6717831927
-3.8255929815 26.8087709382
-3.8957585609 26.9457586837
-3.9659241402 27.0827464292
-4.0360897196 27.2197341747
-4.1062552990 27.3567219202
-4.1764208784 27.4937096657
-4.2465864577 27.6306974112
-4.3167542750 27.7676851567
-4.3120630353 27.6085617007
-4.3073717956 27.4494382448
-4.3026805559 27.2903147889
-4.2979893162 27.1311913330
-4.2932980765 26.9720678771
-4.2886068368 26.8129444212
-4.2839155971 26.6538209653
-4.2792243573 26.4946975094
-4.2745331176 26.3355740534
-4.2698418779 26.1764505975
-4.2651506382 26.0173271416
-4.2604593985 25.8582036857
-4.2557681588 25.6990802298
-4.2510769191 25.5399567739
-4.2463856794 25.3808333180
-4.2416944397 25.2217098620
-4.2370032000 25.0625864061
-4.2323119603 24.9034629502
-4.2276207206 24.7443394943
-4.2229294808 24.5852160384
-4.2182382411 24.4260925825
-4.2135470014 24.2669691266
-4.2088557617 24.1078456706
-4.2041645220 23.9487222147
-4.1994732823 23.7895987588
-4.1947820426 23.6304753029
-4.1900908029 23.4713518470
-4.1853995632 23.3122283911
-4.1807083235 23.1531049352
-4.1760170838 22.9939814793
-4.1713258441 22.8348580233
-4.1666346043 22.6757345674
-4.1619433646 22.5166111115
-4.1572521249 22.3574876556
-4.1525608852 22.1983641997
-4.1478696455 22.0392407438
-4.1431784058 21.8801172879
-4.1384871661 21.7209938319
-4.1337959264 21.5618703760
-4.1291046867 21.4027469201
-4.1244134470 21.2436234642
-4.1197222073 21.0845000083
-4.1150309676 20.9253765524
-4.1103397278 20.7662530965
-4.1056484881 20.6071296406
-4.1009572484 20.4480061846
-4.0962660087 20.2888827287
-4.0915747690 20.1297592728
-4.0868835293 19.9706358169
-4.0821922896 19.8115123610
-4.0775010499 19.6523889051
-4.0728098102 19.4932654492
-4.0681185705 19.3341419932
-4.0634273308 19.1750185373
-4.0587360911 19.0158950814
-4.0540448513 18.8567716255
-4.0493536116 18.6976481696
-4.0446623719 18.5385247137
-4.0399711322 18.3794012578
-4.0352798925 18.2202778018
-4.0305886528 18.0611543459
-4.0258974131 17.9020308900
-4.0212061734 17.7429074341
-4.0165149337 17.5837839782
-4.0118236940 17.4246605223
-4.0071324543 17.2655370664
-4.0024412146 17.1064136105
-3.9977499748 16.9472901545
-3.9930587351 16.7881666986
-3.9883674954 16.6290432427
-3.9836762557 16.4699197868
-3.9789850160 16.3107963309
-3.9742937763 16.1516728750
-3.9696025366 15.9925494191
-3.9649112969 15.8334259631
-3.9602200572 15.6743025072
-3.9555288175 15.5151790513
-3.9508375778 15.3560555954
-3.9461463381 15.1969321395
-3.9414550983 15.0378086836
-3.9367638586 14.8786852277
-3.9320726189 14.7195617718
-3.9273813792 14.5604383158
-3.9226901395 14.4013148599
-3.9179988998 14.2421914040
-3.9133076601 14.0830679481
-3.9086164204 13.9239444922
-3.9039251807 13.7648210363
-3.8992339410 13.6056975804
-3.8945427013 13.4465741244
-3.8898514616 13.2874506685
-3.8851602218 13.1283272126
-3.8804689821 12.9692037567
-3.8757777424 12.8100803008
-3.8710865027 12.6509568449
-3.8663952630 12.4918333890
-3.8617040233 12.3327099330
-3.8570127836 12.1735864771
-3.8523215439 12.0144630212
-3.8476303042 11.8553395653
-3.8429390645 11.6962161094
-3.8382478248 11.5370926535
-3.8335565851 11.3779691976
-3.8288653453 11.2188457417
-3.8241741056 11.0597222857
-3.8194828659 10.9005988298
-3.8147916262 10.7414753739
-3.8101003865 10.5823519180
-3.8054091468 10.4232284621
-3.8007179071 10.2641050062
-3.7960266674 10.1049815503
-3.7913354277 9.9458580943
-3.7866441880 9.7867346384
-3.7819529483 9.6276111825
-3.7772617086 9.4684877266
-3.7725704688 9.3093642707
-3.7678792291 9.1502408148
-3.7631879894 8.9911173589
-3.7584967497 8.8319939029
-3.7538055100 8.6728704470
-3.7491142703 8.5137469911
-3.7444230306 8.3546235352
-3.7397317909 8.1955000793
-3.7350405512 8.0363766234
-3.7303493115 7.8772531675
-3.7256580718 7.7181297116
-3.7209668321 7.5590062556
-3.7162755923 7.3998827997
-3.7115843526 7.2407593438
-3.7068931129 7.0816358879
-3.7022018732 6.9225124320
-3.6975106335 6.7633889761
-3.6928193938 6.6042655202
-3.6881281541 6.4451420642
-3.6834369144 6.2860186083
-3.6787456747 6.1268951524
-3.6740544350 5.9677716965
-3.6693631953 5.8086482406
-3.6646719556 5.6495247847
-3.6599807158 5.4904013288
-3.6552894761 5.3312778729
-3.6505982364 5.1721544169
-3.6459069967 5.0130309610
-3.6412157570 4.8539075051
-3.6365245173 4.6947840492
-3.6318332776 4.5356605933
-3.6271420379 4.3765371374
-3.6224507982 4.2174136815
-3.6177595585 4.0582902255
-3.6130683188 3.8991667696
-3.6083770791 3.7400433137
-3.6036858393 3.5809198578
-3.5989945996 3.4217964019
-3.5943033599 3.2626729460
-3.5896178116 3.1035494901 

Tags: textimportselftruedatadefpltfilename
1条回答
网友
1楼 · 发布于 2024-09-27 00:13:35

你想做的没什么意义。你知道吗

交互模式通过接管主线程运行自己的事件循环来响应鼠标事件等。这对^{}很好,它会阻塞并运行它的事件循环,直到关闭窗口为止。当然,如果你的应用程序有一个GUI,它希望运行自己的事件循环,它就不会运行。你知道吗

show(block=False)被记录为“实验性的”。而且很难看出它如何能够与每个平台上的每个GUI框架很好地集成,而不需要大量的黑客攻击。所以它有一些小故障也就不足为奇了。可能谁也帮不了你。(好吧,如果你能写一个足够详细的bug报告并将其归档到matplotlib,也许有人会想出改进的方法,你会在几个月内得到修复……)

但是,有一个非常简单的解决方法:在它自己的独立进程中运行matplotlib。你知道吗

确切地说,如何设计事物以及将流程边界放在何处取决于您的实验和代码编写,但我的第一个想法是,所有PlotManager都可以成为后台代码的简单代理。这样,您只需要在队列上pickle和发送文件名和几个数字,而不是numpy数组和plot对象之类的东西。(另外,这意味着OnDropText几乎可以立即返回,而不是阻塞GUI读取、处理和打印数组所需的时间。)

您可能会遇到的一个问题是,在python2.x中,Unix(包括macOS)上的multiprocessing通过分叉进程来工作,这会对gui造成严重破坏,而无需一些痛苦的解决方法。如果您安装了PyQt5或PySide2,显然告诉matplotlib使用其中一个可以神奇地解决这些问题,但我还没有证实这一点。如果这不起作用,另一个选择是将multiprocessing的后端口从3.4到2.x,或者(我认为)分叉版本multiprocess。然后可以使用forkserverspawnstart方法,问题就消失了。你知道吗

matplotlib网站上有一些例子,比如this one,以及其他地方。你知道吗

相关问题 更多 >

    热门问题