相关疑难解决方法(0)

推迟python中的函数

在JavaScript中,我习惯于能够调用稍后要执行的函数,就像这样

function foo() {
    alert('bar');
}

setTimeout(foo, 1000);
Run Code Online (Sandbox Code Playgroud)

这不会阻止其他代码的执行.

我不知道如何在Python中实现类似的东西.我可以睡觉

import time
def foo():
    print('bar')

time.sleep(1)
foo()
Run Code Online (Sandbox Code Playgroud)

但这会阻止其他代码的执行.(实际上在我的情况下阻塞Python本身并不是问题,但我无法对该方法进行单元测试.)

我知道线程是为不同步执行而设计的,但我想知道是否更容易,类似setTimeoutsetInterval存在.

python multithreading setinterval

10
推荐指数
2
解决办法
8449
查看次数

“Gui”对象没有属性“after”

我使用了我的工作 tkinter 代码(仅绘制了窗口/按钮等),并尝试从此处已批准的答案中添加一些代码:用于在窗口上打印串行数据的 python 代码。

批准的答案本身只需很小的修改即可工作,但添加到我的代码中时,我收到错误“'Gui'对象没有属性'after'”

我不明白的是为什么在 Gui 类中而不是在 process_serial 方法中查找属性“after”。

from tkinter import *
from tkinter import ttk

import serial
import threading
import queue

class SerialThread(threading.Thread):
    def __init__(self, queue):
        threading.Thread.__init__(self)
        self.queue = queue
    def run(self):
        s = serial.Serial('COM11',115200)
        while True:
            if s.inWaiting():
                text = s.readline(s.inWaiting())
                self.queue.put(text)

class Gui():
    def __init__(self, master):
        ###MAIN FRAME###
        mainFrame = Frame(master, width=50000, height=40000)
        mainFrame.pack(fill = BOTH, expand = 1)

        ###LIST FRAME###
        listFrame = Frame(mainFrame)
        listFrame.pack(side = TOP, fill = BOTH, expand = 1) …
Run Code Online (Sandbox Code Playgroud)

python multithreading tkinter pyserial

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

标签 统计

multithreading ×2

python ×2

pyserial ×1

setinterval ×1

tkinter ×1