窗口关闭后使用tkinter检查按钮值

时间:2018-10-09 17:51:54

标签: python tkinter

我是tkinter的新手。我正在尝试构建一个从脚本启动的弹出表单,该脚本将提供多个复选框,以在关闭窗口后设置脚本中所需的变量。框架打开时我很好,但是我找不到将变量传递出去以备后用的方法。在下面的代码中,我想添加另一个按钮,该按钮将为varPDF和varSHP(全局?)设置这些变量,然后关闭窗口。

  var body = DocumentApp.getActiveDocument().getBody();
  var text = body.editAsText();
  body.clear();

  var tStart = 0;
  text.appendText("Check01\n\n");

  tEnde = body.editAsText().getText().length;
  body.editAsText().setFontSize(tStart, tEnde-1, 33);
  body.editAsText().setForegroundColor(tStart, tEnde-1, '#2E2EFE');
  body.editAsText().setBackgroundColor(tStart, tEnde-1, '#00FFFF');

  tStart = tEnde;
  text.appendText("Check01\n");
  tEnde = body.editAsText().getText().length;
  body.editAsText().setFontSize(tStart, tEnde-1, 22);
  body.editAsText().setForegroundColor(tStart, tEnde-1, '#00FFFF');
  body.editAsText().setBackgroundColor(tStart, tEnde-1, '#2E2EFE');

1 个答案:

答案 0 :(得分:0)

我设法找到了使用全局变量的解决方案,并设置了在单击复选框时对其进行设置的函数。它可能不是最优雅的解决方案,但它可以工作。

##############################################################
# SET GLOBAL VARS
global sVar
sVar = 'STOPIT'
global globPDF
globPDF = 0
global globSHP
globSHP = 0

##############################################################
# INPUT DIALOG
from tkinter import *
from tkinter import ttk

class mainwindow():
    def __init__(self, master):

        def set_globPDF_to_one():
            global globPDF
            globPDF = 1
        def set_globPDF_to_zero():
            global globPDF
            globPDF = 0     

        def set_globSHP_to_one():
            global globSHP
            globSHP = 1
        def set_globSHP_to_zero():
            global globSHP
            globSHP = 0

        def setValues():
            if varPDF.get() == True:
                set_globPDF_to_one()
            else:
                set_globPDF_to_zero()

            if varSHP.get() == True:
                set_globSHP_to_one()
            else:
                set_globSHP_to_zero()

        self.master = master
        window = (self.master)

        ttk.Label(window, text="Select Output:").grid(column=1
                 , row=1, sticky=W)

        varPDF = BooleanVar()
        cbxPDF = Checkbutton(window, text="PDF",
                             variable=varPDF, command=lambda: setValues())
        cbxPDF.grid(column=1, row=2, sticky=W)

        varSHP = BooleanVar()
        cbxSHP = Checkbutton(window, text="SHP",
                             variable=varSHP, command=lambda: setValues())
        cbxSHP.grid(column=1, row=3, sticky=W)

        btnQuit = Button(window,text=u"Quit", command=self.end)
        btnQuit.grid(column=2, row=4, sticky=E, pady=4, padx=10)

        btnSbmt = Button(window,text=u"Submit", command=self.submit)
        btnSbmt.grid(column=1, row=4, sticky=W, pady=4, padx=10)

    def submit(self):
        def set_sVar_to_run():
            global sVar
            sVar = 'COPYIT'

        set_sVar_to_run()
        self.master.destroy()

    def end(self):
        def set_sVar_to_stop():
            global sVar
            sVar = 'STOPIT'

        set_sVar_to_stop()
        print()
        print('Quit')
        self.master.destroy()

def main():
    root = Tk()
    window = mainwindow(root)
    root.lift()
    root.attributes('-topmost',True)
    root.after_idle(root.attributes,'-topmost',True)
    root.mainloop()

if __name__ == '__main__':
    main()

# show output variables
print('sVar: '+sVar)
print('globPDF: '+str(globPDF))
print('globSHP: '+str(globSHP))