wxpython:如何逐个AppendText(按任意键)

时间:2017-07-02 09:41:28

标签: python user-interface wxpython

在这段代码中,我有一个wx TextCtrl来显示信息(info_window),还有一个函数print_info()来接收来自我的主脚本的文本,将它们附加到TextCtrl。文本同时附加,但我需要将它们添加为:

你好1

- 按任意键

你好2

- 按任意键

你好3

用键盘按任意键后,有没有办法逐个追加字符串?

我认为键应该是函数press_any_key()。它可能与任何类型的wx事件有关,但我不知道如何正确编写该函数。

import wx
class Frame(wx.Frame):

    def __init__(self, parent):
        wx.Frame.__init__ (self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size(500,300), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL)

        self.SetSizeHints(wx.DefaultSize, wx.DefaultSize)

        bSizer1 = wx.BoxSizer(wx.VERTICAL)

        self.info_window = wx.TextCtrl(self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.Size(450,250), wx.TE_MULTILINE|wx.TE_READONLY|wx.TE_RICH2)
        bSizer1.Add(self.info_window, 0, wx.ALL, 5)

        self.SetSizer(bSizer1)
        self.Layout()

    def print_info(self, string):
        self.string = string + '\n'
        self.info_window.AppendText(self.string)
        self.press_any_key()

    def press_any_key(self):
        pass


def main():
    frame.print_info('hello 1')
    frame.print_info('hello 2')
    frame.print_info('hello 3')


app = wx.App()
frame = Frame(None)
frame.Show()
main()
app.MainLoop()

感谢萨克森的罗尔夫。我解决了我的问题,关键点是wx.Yield()。我修改了以下代码

import wx
import time

class Frame(wx.Frame):

    def __init__(self, parent):
        wx.Frame.__init__ (self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size(500,300), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL)

        self.SetSizeHints(wx.DefaultSize, wx.DefaultSize)

        bSizer1 = wx.BoxSizer(wx.VERTICAL)

        self.info_window = wx.TextCtrl(self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.Size(450,250), wx.TE_MULTILINE|wx.TE_READONLY|wx.TE_RICH2)
        bSizer1.Add(self.info_window, 0, wx.ALL, 5)
        self.info_window.Bind(wx.EVT_KEY_DOWN, self.go)

        self.SetSizer(bSizer1)
        self.Layout()
        self.go_status = False

    def print_info(self, string):
        self.waitkey()
        self.string = string + '\n'
        self.info_window.AppendText(self.string)
        self.go_status = False

    def go(self, event):
        self.go_status = True


    def waitkey(self):
        while self.go_status == False:
            wx.Yield()
            time.sleep(0.1)


def main():
    frame.print_info('hello 1')
    frame.print_info('hello 2')
    frame.print_info('hello 3')


app = wx.App()
frame = Frame(None)
frame.Show()
main()
app.MainLoop()

1 个答案:

答案 0 :(得分:0)

它不漂亮,但我想不出更简单的方法。

import wx
import time
class Frame(wx.Frame):

    def __init__(self, parent):
        wx.Frame.__init__ (self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size(500,360), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL)
        self.SetSizeHints(500,360)
        bSizer1 = wx.BoxSizer(wx.VERTICAL)
        self.info_window = wx.TextCtrl(self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.Size(450,250), wx.TE_MULTILINE|wx.TE_READONLY|wx.TE_RICH2)
        self.message = wx.StaticText(self,-1,("Press any key"))
        self.keyinput = wx.TextCtrl(self,-1)
        bSizer1.Add(self.info_window, 0, wx.ALL, 5)
        bSizer1.Add(self.message, 0, wx.ALL, 5)
        bSizer1.Add(self.keyinput, 0, wx.ALL, 5)
        self.SetSizer(bSizer1)
        self.Layout()
        self.Show()
        self.keyinput.SetFocus()

    def print_info(self, string):
        self.WaitOnKey()
        self.string = string + '\n'
        self.info_window.AppendText(self.string)


    def WaitOnKey(self):
        while self.keyinput.GetValue() == "":
            wx.Yield()
            time.sleep(0.2)
        self.keyinput.SetValue("")

def main():
    frame.print_info('hello 1')
    frame.print_info('hello 2')
    frame.print_info('hello 3')


app = wx.App()
frame = Frame(None)
main()
app.MainLoop()

wx.Yield()外,代码是自解释的。这会将控制权返回给MainLoop,这意味着程序不会冻结。基本上当程序等待键输入时,控制每隔2/10秒传回主循环,以查看是否还有其他事情发生。这允许程序继续正常执行。

注意:这不适用于功能键。为此,我认为您必须找到bind关键事件的方法并使用event.GetKeyCode()