在Python

时间:2015-09-24 10:01:35

标签: python logging

我想在python中创建一个脚本,当测试运行时会打开一个记录器窗口,我想看到的输出将被推送到记录器窗口。我发现使用wxpython的baisc GUI脚本,但我不知道如何将输出推送到打开的gui。谁能帮我?

我的代码:

import wx
import thread
import threading

class ExamplePanel(wx.Panel):        
        def __init__(self, parent):
            wx.Panel.__init__(self, parent)
            self.quote = wx.StaticText(self, label="Your Log :", pos=(10, 10))

            self.logger = wx.TextCtrl(self, pos=(0,40), size=(1100,1100), style=wx.TE_MULTILINE | wx.TE_READONLY)
        def append_txt(self,txt):
            self.logger.AppendText(txt)




def sample_Window():
    app = wx.App(False)
    frame = wx.Frame(None)
    panel = ExamplePanel(frame)
    frame.Show()
    panel.append_txt("Log Starts Here\n First line of code \n Second line of code ")
    app.MainLoop()

sample_Window()

一旦我给app.Mainloop,我无法进一步输入appendtext方法。我有建议使用线程来运行append_txt作为一个单独的线程传递参数,但我不知道如何做到这一点。我的目标是调用一个方法并将文本作为参数传递,这将在logger窗口中显示文本。

1 个答案:

答案 0 :(得分:0)

最简单的方法是将您的任务嵌入到面板中,然后用一个线程启动它:

Revoke USB debugging authorisations

<强>更新

嗯,这是另一种方法:

import wx
import thread
import threading
import time

class ExamplePanel(wx.Panel):        
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        self.quote = wx.StaticText(self, label="Your Log :", pos=(10, 10))

        self.logger = wx.TextCtrl(self, pos=(0,40), size=(1100,1100), style=wx.TE_MULTILINE | wx.TE_READONLY)

        ########################################################################
        # Use a thread to start your task
        ########################################################################
        task_thread = threading.Thread(target = self.my_task, args = ())
        task_thread.setDaemon(True)
        task_thread.start()

    def append_txt(self,txt):
        self.logger.AppendText(txt)

    def my_task(self):
        ########################################################################
        # Do your job right here and update log
        ########################################################################
        for i in range(100):
            self.append_txt('\nNew line added(No.%s)' % (i + 1))

            time.sleep(1)


def sample_Window():
    app = wx.App(False)
    frame = wx.Frame(None)
    panel = ExamplePanel(frame)
    frame.Show()
    panel.append_txt("Log Starts Here\n First line of code \n Second line of code ")
    app.MainLoop()

sample_Window()