如何在处理过程中更新输出窗口中的任何日志或图形?

2024-04-26 03:58:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用Python做一个自动测试工具。我想实现的一件事是在日志窗口中更新测试日志。但是,所有日志都会在完成测试过程后显示。过程中如何显示日志? 另外,在处理过程中,GUI会停止处理错误消息“notresponding”。如何解决?提前谢谢你的帮助。你知道吗

(窗口7,64位,Python 3.4,tkinter,matplotlib)

==========================================================================================

from tkinter import *
from matplotlib import pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
from time import sleep

root= Tk()
root.title("Auto Test")
root.geometry('300x700')
root.configure(background='white')

datac1_frame = Frame(root,bg="white",width=400, height=30)
datac1_frame.pack(side=TOP, anchor = W,padx= 5,fill=BOTH)

data1_frame = Frame(root,bg="white",width=400, height=280)
data1_frame.pack(side=TOP, anchor = W,padx= 5,fill=BOTH)

log_frame = Frame(root,bg="blue",width=400, height=80)
log_frame.pack(side=TOP, anchor = N,padx= 5,fill = Y)

#Output Box
logmes = Text(log_frame,width=800)
logmes.pack()

class autotest:
    def DUT_config(self):
        sleep(1)
        logmessage('DUT configuration')

    def measurement(self):
        sleep (2)
        logmessage('DUT is under measurement')

def freqentry(frame,text, command = None):
    label = Label(frame,text='Channel', bg = 'chartreuse',
                  width= 10).pack(side = LEFT, padx=10, pady = 2)
    entry = Entry(frame, width = 18, text = text,
                  justify = CENTER).pack(side = LEFT)
    button = Button(frame, text = "Run", bg = "lightsteelblue",
                    width = 5,padx=4, command = command).pack(side = LEFT)

def clickrun():
    logmessage ('button is clicked')
    for i in range(5):
        logmessage('# of try', i)
        at.DUT_config()
        sleep(1)       
        at.measurement()


def logmessage(*args):
    logmes.insert(END, args)
    logmes.insert(END, '\n')
    logmes.see(END)

#Channel, freq, Run button alignment
freqentry(datac1_frame, '2000MHz', clickrun)

## Data Gathering
f_0 = Figure(figsize=(2.5,2.5), dpi =100)
a_0 = f_0.add_subplot(111)
a_0.xaxis.grid(True)
a_0.yaxis.grid(True)
a_0.plot ()

## Display on the plot
canvas = FigureCanvasTkAgg(f_0, data1_frame)
canvas.show()
canvas.get_tk_widget().pack(side=LEFT)

at = autotest()

plt.show()

root.mainloop()

Tags: textfromimportmatplotlibdefsleeprootwidth
1条回答
网友
1楼 · 发布于 2024-04-26 03:58:35

大家好,欢迎来到StackOverflow。你知道吗

我看到的第一个问题是你对范围的处理。我想你把这个贴成Minimal, Complete, and Verifiable example。你知道吗

如果这是您的完整代码,请注意您应该解决一些问题。函数def logmessage(*args)使用变量logmes,该变量在函数之外定义,并在其他函数中额外使用。你知道吗

与大多数编程语言一样,函数的作用域限制了python中变量的访问。如果要使用声明的logmeslistbox“globally”,还需要通过在函数中声明变量global来告诉函数(下面的示例)



    # We are inside the main routine here - global scope
    root= Tk()

    log_frame = Frame(root,bg="blue",width=400, height=80)
    log_frame.pack(side=TOP, anchor = N,padx= 5,fill = Y)

    #Output Box
    logmes = Text(log_frame,width=800)
    logmes.pack()

    # Here come functions having their own scope
    def logmessage(*args):
        # Use variables from global scope - global declaration
        #################################
        global logmes
        #################################
        logmes.insert(END, args)
        logmes.insert(END, '\n')
        logmes.see(END)

使用全局变量通常被视为一种坏习惯,因为它减少了在其他目的中重用代码的方法。我个人更喜欢在模块中使用类,把所有属于类的东西放在一个类中。这将产生一个模块结构,如

  • 模块标题:例如评论行、版本、作者、社邦等。

  • 导入部分:在这里导入所需内容

  • 类和函数部分:在这里我定义了我的类和函数

  • 主例程:从if __name__=="__main__":开始我开始处理如果脚本被调用而没有导入,函数调用只在这里发生

不幸的是,我也看到了第二个问题—在“导入部分”中,您正在导入from matplotlib import pyplot as plt。稍后在主例程中调用plt.show。我不经常使用matplotlib,但是如果我没记错的话,首先定义一个pyplot Object,然后调用对象上的show例程,而不是模块本身。你知道吗

在python控制台输出中是否收到任何错误消息?(假设您是从命令行调用脚本)还是双击它来调用它?如何存储文件?我在这里重点讨论文件扩展名,因为在有或没有控制台输出窗口时,如果将其存储为*.pyw*.py会产生影响。你知道吗

我刚刚认识到的另一个问题是你的类定义。正如documentation of the class module in python所说,类是支持

two kinds of operations: attribute references and instantiation.

你把你的类当作一个函数包装器。您没有使用继承,也没有声明__init__函数来实例化类变量。你知道吗

毫无疑问,python为您提供了这样做的能力,但是请记住,您使用class做的事情比使用module做的事情更多。为不需要超过传递的参数(如果有)的某些函数声明命名空间。你知道吗

我真的建议你仔细看看class documentation。这确实有助于改进您的代码,并通过提问和等待文档可以直接告诉您的内容的回复以及使用简单的示例来减少被拖慢的必要性。你知道吗

请不要把我的最后一句话看作是冒犯,因为这绝对不是为了在这里,只是作为一个建议,以帮助您提高您的工作。你知道吗

相关问题 更多 >