wxPython应用程序:没有错误,但仍然冻结 [英] wxPython app: No error but still freezes

查看:85
本文介绍了wxPython应用程序:没有错误,但仍然冻结的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码没有任何错误,但是在我按下垃圾邮件按钮后,它冻结了,什么也没发生.有人看到代码有什么问题吗?

I don't get any errors with my code, but after I hit the spam button it freezes and nothing happens. Does anyone see anything wrong with the code?

    import Skype4Py, wx, time as t


    skype = Skype4Py.Skype()
    skype.Attach()
    name = ""
    chat = ""
    message = ""


    class skyped(wx.Frame):

        def __init__(self,parent,id):
            wx.Frame.__init__(self,parent,id,"Skype Chat Spammer",size=(300,200))
            panel=wx.Panel(self)
            start=wx.Button(panel,label="Spam!",pos=(140,100),size=(50,20))
            stop=wx.Button(panel,label="Stop!", pos=(200,100),size=(50,20))
            self.Bind(wx.EVT_BUTTON, self.spam, start)
            self.Bind(wx.EVT_CLOSE, self.closewindow)
            entered=wx.TextEntryDialog(None, "User to spam?", "User", "Username Here")
            if entered.ShowModal()==wx.ID_OK:
                name=entered.GetValue()
            entered1=wx.TextEntryDialog(None, "Message?", "Message", "Message Here")
            if entered1.ShowModal()==wx.ID_OK:
                message=entered1.GetValue()

        def spam(self,event):
            global chat
            global name
            global message
            chat = skype.CreateChatWith(name)
            while 1:
                chat.SendMessage(message)

        def closewindow(self,event):
            self.Destroy()

    if __name__=="__main__":
        app=wx.PySimpleApp()
        frame=skyped(parent=None,id=-1)
        frame.Show()
        app.MainLoop()

推荐答案

建立在kotlinski的答案的基础上,是的,它冻结了,因为您正在应用程序的主线程中进行无休止的循环.该应用程序无法再处理任何与gui相关的交互或事件.

Building off of kotlinski's answer, yes its freezing because you are doing an endless loop in the main thread of your application. The app can no longer process any gui-related interaction or events.

虽然我对wx不太了解,但理论与PyQt相同.您永远不要通过长时间运行或繁重的操作来阻塞应用程序的主线程.这些应该在单独的线程中运行,并与信号回传:

While I don't know much wx, the theory is the same as PyQt. You should never block the main thread of your app with any long running or heavy lifting operations. Those should be run in separate threads and communicating back with signals:

http://wiki.wxpython.org/LongRunningTasks

您的主线程应始终保持清晰,以处理用户与小部件的交互.

Your main thread should always be clear to handle user interaction with the widgets.

这篇关于wxPython应用程序:没有错误,但仍然冻结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆