Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
wxpython:如何逐个追加文本(按任意键时)_Python_User Interface_Wxpython - Fatal编程技术网

wxpython:如何逐个追加文本(按任意键时)

wxpython:如何逐个追加文本(按任意键时),python,user-interface,wxpython,Python,User Interface,Wxpython,在这段代码中,我有一个wx TextCtrl来显示信息(info_窗口),还有一个函数print_info()来接收来自主脚本的文本,并将它们附加到TextCtrl。文本是同时附加的,但我需要像这样附加: 你好1 -按任意键 你好2 -按任意键 你好3 用键盘按任意键后,有没有办法逐个追加字符串 我想这个键应该是函数press\u any\u key()。它可能与任何类型的wx事件相关联,但我不知道如何正确编写函数 import wx class Frame(wx.Frame): de

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

你好1

-按任意键

你好2

-按任意键

你好3

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

我想这个键应该是函数press\u any\u 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()

这并不漂亮,但我想不出更简单的方法

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秒被传递回主循环,以查看是否还有其他情况发生。这允许程序继续正常执行


注意:这不适用于功能键。为此,我认为您必须找到一种方法,将
绑定到一个键事件,并使用
event.GetKeyCode()

谢谢!函数wx.Yield()是一个很好的提示!我修改了代码,将wx.EVT_KEY_向下绑定,并将go()函数绑定到info_窗口。现在代码完全按照我的方式运行。很高兴能提供帮助