Orange的扩展插件Widgets开发(五)-Utilities

半世苍凉 提交于 2020-04-06 23:52:58

Utilities

Progress Bar

Operations that take more than a split second indicate their progress with a progress bar

_images/progressbar.png

and in the title bar of the widget’s window.

_images/progressbar-title.png

There are three mechanisms for implementing this.

Context handlers

The recommended and the simplest approach is to wrap the code into context handler progressBar.

If the operation consists of a known number of steps that take similar time, the context handler can be used as follows:

with self.progressBar(n_steps) as progress:
    for k in range(n_steps):
        self.do_some_processing()
        progress.advance()

In general, the argument ofself.progressBarrepresents the total towards which the progress bar advances. Methodprogress.advance()accepts an argument representing the advance.

with self.progressBar(total) as progress:
    for k in s:
        self.do_some_processing()
        progress.advance(adv)

In this case, the values ofadvshould sum up to thetotal;advandtotaldo not need to be integer numbers.

Iftotalis omitted, it defaults to 1, so the advances must sum to 1.

The progress can also be set to absolute values by calling the widget’s methodsself.progressBarSet(perc). This method belongs to the group of methods for direct manipulation of the progress bar (see below) and expects the state in percents (0 to 100).

with self.progressBar():
    ...
    self.progressBarSet(perc)
    ...

Progress bar class

ClassOrange.widgets.gui.ProgressBaris initialized with a widget and the number of iterations:

progress = Orange.widgets.gui.ProgressBar(self, n)

is equivalent to

with self.progressBar(n) as progress:

This form is useful when the initialization of the progress bar and setting of its state are not in the same block.

Direct manipulation of progress bar

The progress bar can be manipulated directly through functions

  • progressBarInit(self)

  • progressBarSet(self, p)

  • progressBarFinished(self)

progressBarInitinitializes the progress bar,progressBarSetsets it toppercents (from 0 to 100), andprogressBarFinishedcloses it.

The code that uses these methods must use a try-except or try-finally block to ensure that the progress bar is removed if the exception is raised.

Issuing warning and errors

Widgets can show information messages, warnings and errors. These are displayed in the top row of the widget and also indicated in the schema.

_images/warningmessage.png

Messages, warning and errors are issued by methodsself.information(id, text),self.warning(id, text)andself.error(id, text), respectively. A warning like the one above is issued by

self.warning(42, "Data contains continuous variables. "
             "Discretize the data to use them.")

and removed by

self.warning(42)

A widget can have multiple such messages at the same time. They are distinguished by different id’s, like42above. The id numbers are arbitrary and are assigned by the programmer. They need to be unique for a widget.

Multiple error messages can be removed by calling the function with a list of id’s,self.warning([1, 13, 42]).

Since most widgets have only a single error message, the id can be omitted (in which case it defaults to 0). In this case,

self.warning("Data contains continuous variables. "
             "Discretize the data to use them.")

issues the warning, and

self.warning()

removes it.

Tips

Widgets can provide tips about features that are not be obvious or exposed in the GUI.

_images/usertips.png

Such messages are stored in widget’s class attributeUserAdviceMessages. When a widget is first shown, a message from this list is selected for display. If a user accepts (clicks ‘Ok. Got it’) the choice is recorded and the message is never shown again; just closing the message will not mark it as seen. Messages can be displayed again by pressing Shift + F1.

UserAdviceMessagescontains instances ofMessage. The messages contains a text and an id (also a string), and, optionally, an icon and an URL with further information.

The confusion matrix widget sets up the following list:

UserAdviceMessages = [
    widget.Message("Clicking on cells or in headers outputs the "
                       "corresponding data instances",
                   "click_cell")]


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!