如何更改检查按钮中的变量状态并将其用于另一个功能

问题描述 投票:0回答:1

我在主窗口中有一个检查按钮。单击复选按钮时,我需要使用两个功能。第一个函数打开一个弹出窗口,第二个函数将“activate_SPI”变量从“inactive”更改为“active”。我想在另一个函数中使用“activate_SPI”状态,当我按下 GUI 中的“计算”按钮以打印“确定”时调用该函数。但是,我遇到以下错误:

“NameError:名称'activate_SPI'未定义”

我该如何解决这个错误?

import pandas as pd
import numpy as np
from standard_precip import spi
import tkinter as tk
from tkinter import ttk
from tkinter import filedialog
from tkinter.messagebox import showinfo
import os
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg, 
NavigationToolbar2Tk)
from tkinter import * 
from PIL import Image, ImageTk



class EGEDT(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.master = master
        self.pack()
        self.create_widgets()
        # dimensions of the main window
        master.geometry("300x300")

    def create_widgets(self):
        self.top_frame = ttk.Frame(self)

        padding_x = 8
        padding_y = 7
        self.working_dir = "/"
               
        # Create a check button   
        self.ab_frame = ttk.Frame(self.top_frame)
        self.ab_label = ttk.Label(self.ab_frame, text="Select the check button:")
        self.ab_label.grid(row=1, column=0, padx= 5, pady= 5)
        
        var1 = tk.IntVar()
        
        self.a = ttk.Checkbutton(self.ab_frame,text="SPI",variable=var1, onvalue=1, offvalue=0,command=lambda: [f() for f in [self.open_popup_SPI, self.func_activate_SPI]])
        self.a.grid(row=2, column=1, padx= 0, pady= 0)
        self.ab_frame.grid(row=2, column=0, sticky=tk.W, padx=padding_x, pady=padding_y)       
        self.top_frame.grid(row=0, column=0)
        
        # Output file widget
        self.out_frame = ttk.Frame(self.top_frame)
        self.out_label = ttk.Label(self.out_frame, text="Save : ")
        self.out_label.grid(row=0, column=0, sticky=tk.W)
        self.out_string = tk.StringVar()
        self.out_entry = ttk.Entry(self.out_frame, width=7, textvariable=self.out_string)
        self.out_entry.grid(row=1, column=0)
        self.out_btn = ttk.Button(self.out_frame, width=7, text="...", command=self.out_file_selector)
        self.out_btn.grid(row=1, column=1)
        self.out_frame.grid(row=5, column=0, sticky=tk.W, padx=padding_x, pady=padding_y)
        
        # Create ok button to save the SPI excek file into the hard drive
        self.buttons = ttk.Frame(self.out_frame)
        self.PDF = ttk.Button(self.out_frame, text="Compute", width=10, command=self.Compute_TI_model_and_Extract_EG)
        self.PDF.grid(row=1, column=2)
        self.buttons.grid(row=5, column=0, sticky=tk.E, padx=padding_x, pady=padding_y)
        
        self.buttons = ttk.Frame(self.out_frame)
        self.cancel = ttk.Button(self.buttons, text="Cancel", width=7, command=self.master.destroy)
        self.cancel.grid(row=2, column=2)
        self.buttons.grid(row=2, column=2, sticky=tk.E, padx=padding_x, pady=padding_y)
        
    def open_popup_SPI(self):
       top= Toplevel(self.top_frame)
       top.geometry("750x250")
       top.title("Child Window")
       Label(top, text= "Hello World!", font=('Mistral 18 bold')).place(x=150,y=80)
       
    activate_SPI = "inactive"
       
    def func_activate_SPI(activate_SPI):
        # global activate_SPI  
        activate_SPI = "active"
        
    def Geotiff_file_selector(self):
        result = self.Geotiff_string.get()
        result = filedialog.askopenfilename(initialdir = self.working_dir, title = "Select File", filetypes = (("Geotiff files","*.tif"),("all files","*.*")))
        self.Geotiff_string.set(result)
        self.working_dir = os.path.dirname(result)
        
    def out_file_selector(self):
        result = self.out_string.get()
        result = filedialog.asksaveasfilename(initialdir = self.working_dir, title = "Save File", filetypes = (("Geotiff files","*.tif"),("all files","*.*")))
        self.out_string.set(result)
        self.working_dir = os.path.dirname(result)
        
    def Compute_TI_model_and_Extract_EG(self):
        
        if activate_SPI == 'active':
            print('ok')
            
        
def main():
    root = tk.Tk()   
    root.title("Main window")
    app = EGEDT(master=root)
    app.mainloop()

main()
python function tkinter
1个回答
0
投票

由于

activate_SPI
是一个类变量,它可以通过使用
EGEDT.activate_SPI
来访问。

但是我建议使用 instance variable 代替局部变量

var1

class EGEDT(tk.Frame):
    ...
    def create_widgets(self):
        ...
        self.activate_SPI = tk.IntVar()  # replace var1

        self.a = ttk.Checkbutton(self.ab_frame, text="SPI", variable=self.activate_SPI, 
                                 onvalue=1, offvalue=0, command=self.open_popup_SPI)
        ...

    ...
    #activate_SPI = "inactive"   # don't use class variable

    # this function is not necessary at all
    def func_activate_SPI(self):
        pass

    ...
    def Compute_TI_model_and_Extract_EG(self):
        if self.activate_SPI.get():
            print("ok")
© www.soinside.com 2019 - 2024. All rights reserved.