使用WXPython在GUI中的瞬态文本

时间:2012-08-31 20:42:56

标签: python wxpython

我正在处理一个简单的GUI,它输出一个自动复制到剪贴板的数值。我试图在发生这种情况时立即将“复制”文本闪烁,立即显示,然后在一两秒后消失。

文本会按照我的要求闪烁,但显示的输出字段在文本消失之前不会更新,这不是我编写脚本的顺序。目标是让输出字段看起来在文本出现的同一时刻更新。

我出现文字的方式相当粗糙,所以我对建议很开放。意见和评论将不胜感激。

import sys
from win32gui import GetWindowText, EnumWindows, ShowWindow, SetForegroundWindow
import win32clipboard
import win32con
import time
import wx
from wxPython.wx import *
import itertools
from time import sleep


class MainFrame(wx.Frame):
    def __init__(self,title):

        wx.Frame.__init__(self, None, title="RRESI Rounder", pos=(0,0), size=(210,160))
        panel=Panel(self)


class Panel(wx.Panel):

    def __init__(self, parent):
        wx.Panel.__init__(self, parent)

        x1=10; x2=110
        y1=10; dy=30; ddy=-3
        boxlength=80

        self.label1 = wx.StaticText(self, label="Input Number:", pos=(x1,y1+dy*1))
        self.Input = wx.TextCtrl(self, value="100.00001", pos=(x2,ddy+y1+dy*1), size=(boxlength,-1))

        self.button =wx.Button(self, label="GO", pos=(9999,y1+dy*3))
        self.Bind(wx.EVT_BUTTON, self.OnClick,self.button)
        self.button.SetDefault()

        self.label0 = wx.StaticText(self, label="Round to closest:  1/", pos=(x1,y1+dy*0))
        self.Denominator = wx.TextCtrl(self, value="64", pos=(x2,ddy+y1+dy*0), size=(boxlength,-1))

        self.label2 = wx.StaticText(self, label="Output Number:", pos=(x1,y1+dy*2))
        self.display = wx.TextCtrl(self, value="100.00000", pos=(x2,ddy+y1+dy*2), size=(boxlength,-1))
        self.display.SetBackgroundColour(wx.Colour(232, 232, 232))

        self.label3 = wx.StaticText(self, label="          ", pos=(x2+7,y1+dy*2+20)) #Copied

        self.label4 = wx.StaticText(self, label="", pos=(x2+7,y1+dy*2))
        self.label4.SetBackgroundColour(wx.Colour(232, 232, 232))

    def OnClick(self,event):      

        def openClipboard():
            try:
                win32clipboard.OpenClipboard()
            except Exception, e:
                print e
                pass

        def closeClipboard():
            try:
                win32clipboard.CloseClipboard()
            except Exception, e:
                print e
                pass

        def clearClipboard():
            try:
                openClipboard()
                win32clipboard.EmptyClipboard()
                closeClipboard()
            except TypeError:
                pass

        def setText(txt): 
            openClipboard()
            win32clipboard.EmptyClipboard()
            win32clipboard.SetClipboardText(txt) 
            closeClipboard()

        Denominator = float(self.Denominator.GetValue())
        Input=float(self.Input.GetValue())
        Output=round(Input*Denominator,0)/Denominator
        self.display.SetValue(str(Output))
        setText(str(Output))
        self.label4.SetLabel(str(Output)+"")
        self.label3.SetLabel("Copied")
        sleep(.5)
        self.label3.SetLabel("                    ")


if __name__=="__main__":
    app = wx.App(redirect=False)   # Error messages don't go to popup window
    frame = MainFrame("RRESI Rounder")
    frame.Show()
    app.MainLoop()

1 个答案:

答案 0 :(得分:1)

你只需要强制重绘,否则它不会重新绘制,直到调用下一个EVT_PAINT(在函数退出...之后)睡眠阻塞...这意味着在睡眠时根本没有执行任何代码< / p>

...
self.label3.SetLabel("Copied")
self.Update()#force redraw
....
相关问题