全局变量过程

时间:2018-02-03 16:51:12

标签: python user-interface variables tkinter global

我正在使用Tkinter创建GUI的重置按钮。我的目的是将标签的所有值重置为0或消失。

要重置标签的值,我将标签定义为全局变量。

const distribute = _.curry(function* (genIteratorIndex, iterators, mainIterator) {
    let iteratorIndex = 0;
    let done = [];

    // instantiate iterators
    for(let iterator of iterators) {
        iterators[iteratorIndex] = iterator(mainIterator);
        done.push(false);
        iteratorIndex++;
    }

    // Pass stuff through
    while(true) {
        let next = iterators[genIteratorIndex.next().value].next();
        done[iteratorIndex] = next.done;
        if(!next.done) {
            yield next.value;
        }
        if(done.every(done => done === true)) {
            return;
        }
    }
});

function* subSequent(len) {
    let curr = 0;
    while(true) {
        if(curr === len) {
            curr = 0;
        }
        yield curr;
        curr++;
    }
}


let things = chain([
    gen,
    addOne,
    distribute(subSequent(3), [
        addOne,
        addTwo,
        addThree
    ]),
    addOne
]);

主要功能:

labelP = None
labelV = None
labelH = None
labelT = None

重置功能:

def predict():
    global labelP
    global labelV
    global labelH
    global labelT
...
    labelP = Label(mainframe, text = np.array(A[1,0]), width=5, font = 'Arial 10 bold').grid(column=2, row=5, sticky=W)
    labelV = Label(mainframe, text = np.array(A[1,1]), width=5, font = 'Arial 10 bold').grid(column=2, row=6, sticky=W)
    labelH = Label(mainframe, text = np.array(A[1,2]), width=5, font = 'Arial 10 bold').grid(column=2, row=7, sticky=W)
    labelT = Label(mainframe, text = np.array(A[1,3]), width=5, font = 'Arial 10 bold').grid(column=2, row=8, sticky=W)
...

重置按钮:

def reset():
    global labelP
    global labelV
    global labelH
    global labelT

    labelP.delete()
    labelV.delete()
    labelH.delete()
    labelT.delete()

虽然我也尝试了btn2 = Button(mainframe, text="Reset", command = reset).grid(column=1, row=9, sticky=W) labelP.config()labelP.set(""),但我总是收到错误:

labelP.destroy()

我应该为全局变量定义什么?

谢谢 这是我的代码:https://gist.github.com/TonyS0n/8ee8db392196a405c2ef27ac44df3059

1 个答案:

答案 0 :(得分:1)

您无法使用单行初始化和布局窗口小部件。你必须把它放在2行上,如下所示:

labelP = Label(mainframe, text = np.array(A[1,0]), width=5, font = 'Arial 10 bold')
labelP.grid(column=2, row=5, sticky=W)