将字符串本地复制到剪贴板python 3

时间:2016-12-08 00:53:45

标签: python windows clipboard

我找了很长时间,只使用python中包含的目录,将一个字符串放在剪贴板上的好方法。我试过了

import subprocess
def copy2clip(txt):
    cmd='echo '+txt.strip()+'|clip'
    return subprocess.check_call(cmd, shell=True)

然后调用

copy2clip('text')

然而,这似乎在剪贴板上的文本中添加了一条额外的行。

我也尝试过Tkinter方法,但是当我尝试粘贴时,它只是让python窗口崩溃了。

我在Windows 10上运行python 3.5.2。

1 个答案:

答案 0 :(得分:4)

我用过

public void  ColorAllQuery()
{
    Thread t = new Thread(() =>
    {
        this.Dispatcher.Invoke(new Action(delegate { 
            TextRange range = new TextRange(Richtext.Document.ContentStart, Richtext.Document.ContentEnd);
            range.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(Colors.White));

            Regex FunctionsR = new Regex(Functions, RegexOptions.Compiled | RegexOptions.IgnoreCase);
            Regex Variables = new Regex(Variables, RegexOptions.Compiled | RegexOptions.IgnoreCase);
            Regex Tables = new Regex("(" + TablesNames + ")", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            Regex Columns = new Regex("(" + ColumnsNames + ")", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            Regex Qutation = new Regex(@"'.*?'|n'.*?'|"".*?""|n"".*?""", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            Regex Comment = new Regex(@"(--.*)", RegexOptions.Compiled | RegexOptions.IgnoreCase);

            List<TextPointer> pointer = new List<TextPointer>() ;
            List<Match> matchs = new List<Match>();
            List<Brush> color = new List<Brush>();

            var start = Richtext.Document.ContentStart;

            while (start != null && start.CompareTo(Richtext.Document.ContentEnd) < 0)
            {
                if (start.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text)
                {
                    var match = QuerysR.Match(start.GetTextInRun(LogicalDirection.Forward));
            var match2 = FunctionsR.Match(start.GetTextInRun(LogicalDirection.Forward));
            var match3 = Varubls.Match(start.GetTextInRun(LogicalDirection.Forward));
            var match4 = Tables.Match(start.GetTextInRun(LogicalDirection.Forward));
            var match5 = Columns.Match(start.GetTextInRun(LogicalDirection.Forward));
            var match6 = Quotation.Match(start.GetTextInRun(LogicalDirection.Forward));
            var match7 = Comment.Match(start.GetTextInRun(LogicalDirection.Forward));
            pointer.AddRange(new TextPointer[] { start, start, start, start, start, start, start });
            matchs.AddRange(new Match[] { match, match2, match3, match4, match5, match6, match7 });
            color.AddRange(new Brush[] { new SolidColorBrush(Color.FromArgb(255, 0, 120, 255)), Brushes.DeepPink, Brushes.LightGreen, Brushes.Orange, Brushes.Gray, Brushes.Red, Brushes.Green });
        }
        start = start.GetNextContextPosition(LogicalDirection.Forward);
    }

    Thread OT = new Thread(() => ColorGatherdValues(pointer, matchs, color));
    OT.Start();
    }));
    });
    t.Start();
}

private void ColorGatherdValues(List<TextPointer> pointer, List<Match> matchs, List<Brush> color)
{
        for (int i = 0; i < pointer.Count; i++)
        {
            var textrange = new TextRange(pointer.ToArray()[i].GetPositionAtOffset(matchs.ToArray()[i].Index, LogicalDirection.Forward), pointer.ToArray()[i].GetPositionAtOffset(matchs.ToArray()[i].Index + matchs.ToArray()[i].Length, LogicalDirection.Backward));
            textrange.ApplyPropertyValue(TextElement.ForegroundProperty, color.ToArray()[i]);
        }
}

工作得很好。感谢@eryksun评论这个答案。