如何在wxpython中防止gui冻结?

时间:2015-12-14 12:22:16

标签: python multithreading wxpython

[问题] 如果我点击一个触发某个功能的按钮,那么gui将会冻结,直到该功能结束。

[CODE]

import wx
app = wx.App(redirect=False)
top = wx.Frame(None)
top.Maximize(True) # Set to maximize the application
sizer = wx.GridBagSizer()

def testFunction(event):
    import pyautogui
    import time
    pyautogui.FAILSAFE = False
    for i in range(2):
            pyautogui.hotkey('win','r')
            time.sleep (0.5)
            pyautogui.typewrite('cmd.exe')
            time.sleep (0.5)
            pyautogui.hotkey('enter')
            time.sleep (0.5)
            time.sleep (3)


addButton = wx.Button( top, -1, "Start", style=wx.BU_EXACTFIT )
sizer.Add(addButton, (6, 8), (2, 14), wx.EXPAND)
top.Bind(wx.EVT_BUTTON, testFunction, addButton)
top.Sizer = sizer
top.Sizer.Fit(top)
top.Show()
app.MainLoop()

[当前] gui冻结直到功能结束。

[DESIRED] gui不应该冻结。 注意:我认为这与线程有关但我无法理解这个概念。

3 个答案:

答案 0 :(得分:1)

嗯,这对我有用:

# -*- coding: utf-8 -*-
import wx
app = wx.App(redirect=False)
top = wx.Frame(None)
top.Maximize(False) # Set to maximize the application
sizer = wx.GridBagSizer()

def testFunction(event):
    import time
    for i in range(2):
        print ('win','r')
        time.sleep (0.5)
        print ('cmd.exe')
        time.sleep (0.5)
        print ('enter')
        time.sleep (0.5)
        print 'sleep'
        time.sleep (3)
        print u"Iteración %d".format(i+1)

def thread_start(event):
    import threading
    th = threading.Thread(target=testFunction, args=(event,))
    th.start()



addButton = wx.Button( top, -1, "Start", style=wx.BU_EXACTFIT )
sizer.Add(addButton, (6, 8), (2, 14), wx.EXPAND)
# top.Bind(wx.EVT_BUTTON, testFunction, addButton)
top.Bind(wx.EVT_BUTTON, thread_start, addButton)
top.Sizer = sizer
top.Sizer.Fit(top)
top.Show()
app.MainLoop()

你有一个不打怪的gui。 你可以从那开始并添加输入变量(比如获取和识别你的线程,这样你就可以知道正在调用哪个线程)。

我删除了py2autogui库,因为我没有安装它(并且不需要这个例子)。

答案 1 :(得分:0)

使用wx.CallLater,它会在给定时间后使用参数调用给定的callable。函数testFunction的结构应该改变:

def testFunction(event):
    import pyautogui
    pyautogui.FAILSAFE = False

    def step1(i):
        pyautogui.hotkey('win','r')
        wx.CallLater(500, step2, i)
    def step2(i):
        pyautogui.typewrite('cmd.exe')
        wx.CallLater(500, step3, i)
    def step3(i):
        pyautogui.hotkey('enter')
        if i <= 1:
            return
        wx.CallLater(3500, step1, i-1)

    step1(2)

答案 2 :(得分:-1)

我不熟悉wx,但是mabye这有帮助

import wx
import threading
app = wx.App(redirect=False)
top = wx.Frame(None)
top.Maximize(True) # Set to maximize the application
sizer = wx.GridBagSizer()

def testFunction(event):
    import pyautogui
    import time
    pyautogui.FAILSAFE = False
    for i in range(2):
            pyautogui.hotkey('win','r')
            time.sleep (0.5)
            pyautogui.typewrite('cmd.exe')
            time.sleep (0.5)
            pyautogui.hotkey('enter')
            time.sleep (0.5)
            time.sleep (3)

t1 = threading.Thread(target=testFunction, args=[])    

addButton = wx.Button( top, -1, "Start", style=wx.BU_EXACTFIT )
sizer.Add(addButton, (6, 8), (2, 14), wx.EXPAND)
top.Bind(wx.EVT_BUTTON, t1, addButton)
top.Sizer = sizer
top.Sizer.Fit(top)
top.Show()
app.MainLoop()