小编Joe*_*Joe的帖子

用键盘中断关闭所有线程

我在这里尝试做的是使用键盘中断退出程序中所有正在进行的线程.这是我的代码的简化版本,其中创建了线程:

for i in taskDictionary:
    try:
        sleep(60)
        thread = Thread(target = mainModule.executeThread)
        thread.start()
    except KeyboardInterrupt:
        thread.__stop()
Run Code Online (Sandbox Code Playgroud)

程序本身要复杂得多,占据了影响线程的大量不同变量,甚至可以选择以顺序模式启动,其中任务没有线程化,而是逐个启动,因此可能存在一些问题我只是想起了这个小小的变化.我以产生50/50结果的方式完成了这项工作.中断可以工作,但线程永远不会干净地退出.有时它们会继续运行但是停止执行未来的线程,有时候它们会因为中断的大量错误而退出,有时中断根本不会执行任何操作.上次我运行它时,程序停止执行任何未来的线程,但没有停止当前线程.有没有办法退出线程而不进入线程实际执行的模块?

python multithreading

37
推荐指数
2
解决办法
4万
查看次数

JTextPane中的可单击文本

我有一个声明如下的JTextPane:

JTextPane box = new JTextPane();
JScrollPane scroll = new JScrollPane();
StyledDocument doc = box.getStyledDocument();
scroll.setViewportView(box);
scroll = new JScrollPane(box);
Run Code Online (Sandbox Code Playgroud)

我正在附加文本如下:

public void appendChatText(String text)
{   
    try
    {
        doc.insertString(doc.getLength(), text, null);
        box.setAutoscrolls(true);
        box.setCaretPosition(box.getDocument().getLength());    
    }
    catch(BadLocationException e)
    {
        e.printStackTrace();
    }
}
Run Code Online (Sandbox Code Playgroud)

我还设法轻松地让JTextPane根据需要显示图像和组件,但我无法弄清楚如何将可点击文本编码到JTextPane中.例如,我希望它打印一条消息,上面写着"文件上传到服务器.接受*拒绝*",如果用户点击接受或拒绝字符串,则它会执行相应的功能.关于如何有效实现这一目标的任何想法?

java url swing jtextpane

7
推荐指数
1
解决办法
6481
查看次数

从python中的字典列表中选择一个字段

假设我有一个字典列表,如下所示:

dictionList = {1: {'Type': 'Cat', 'Legs': 4},
               2: {'Type': 'Dog', 'Legs': 4},
               3: {'Type': 'Bird', 'Legs': 2}}
Run Code Online (Sandbox Code Playgroud)

使用for循环我想遍历列表,直到我找到一个Type字段等于的字典"Dog".我最好的尝试是:

 for i in dictionList:
     if dictionList(i['Type']) == "Dog":
         print "Found dog!"
Run Code Online (Sandbox Code Playgroud)

但这给我带来了以下错误:

TypeError: 'int' object has no attribute '__getitem__'
Run Code Online (Sandbox Code Playgroud)

关于如何正确地做到这一点的任何想法?

python dictionary for-loop list

3
推荐指数
1
解决办法
6190
查看次数

标签 统计

python ×2

dictionary ×1

for-loop ×1

java ×1

jtextpane ×1

list ×1

multithreading ×1

swing ×1

url ×1