DenGor
DenGor

Reputation: 205

Simultaneous use of a checkbutton and a button

I am new to Python and obviously missing something important when working with checkbuttons. Here's the idea behind my program: I select a file manually and then, depending on whether the checkbox is checked or not, trigger either one calculation sequence or another using a button. To achieve this, I wanted to verify the state of the checkbox using the .get() command.

What I've found is that only one sequence is triggered all the time independently of the state of the checkbox. .get() is not updated when I click on the checkbox. What am I doing wrong? Any help will be greatly appreciated.

from tkinter import *
from tkinter import filedialog
import tkinter as tk

master = Tk()
root = tk.Tk()

col_sep = "\t"

col_h_b = []  # field column for background
col_m_b = []  # magnetization column for background

def choose_b_file():
    root.fileName_b = filedialog.askopenfilename(filetypes=((".dat files", "*.dat"), ("All files", "*.*")))
    with open(root.fileName_b, 'r') as f:
         for line in f:
            if line[0] != "#":
                linedata = [float(line.split(col_sep)[i]) for i in range(len(line.split(col_sep)))]
                col_h_b.append(linedata[4])
                col_m_b.append(linedata[5])
    print(f.name)

offset = BooleanVar()
checkbox = tk.Checkbutton(root, text="offset subtraction", variable=offset,onvalue=1, offvalue=0)
checkbox.pack()

def plot():
    if offset.get() == 1:
        #some mathematical operations and graph plotting
    else:
        #other mathematical operations and graph plotting

def close_window():
    exit()

b_data = Button(master, text="Background", width=20, command=choose_b_file)
m_minus_b = Button(master, text="Plot", width=5, command=plot)
quit = Button(master, text="Quit", width=5, command=close_window)

b_data.pack()
m_minus_b.pack()
quit.pack()

root.mainloop()

Upvotes: 1

Views: 750

Answers (1)

PRMoureu
PRMoureu

Reputation: 13347

You are mostly messing parents widgets root and master. Do you need to have a separate window for the checkbox ?

Otherwise the quick fix is to replace root by master in the checkbox creation :

checkbox = tk.Checkbutton(root, text="offset subtraction" ...)

You can also simplify the boolean stuff, the default behavior for the checkbutton is to use 0 and 1, and remove the master or the root, choose only one.

from tkinter import *
from tkinter import filedialog

root = Tk()

col_sep = "\t"    
col_h_b = []  # field column for background
col_m_b = []  # magnetization column for background

def choose_b_file():
    root.fileName_b = filedialog.askopenfilename(filetypes=((".dat files", "*.dat"), ("All files", "*.*")))
    with open(root.fileName_b, 'r') as f:
         for line in f:
            if line[0] != "#":
                linedata = [float(line.split(col_sep)[i]) for i in range(len(line.split(col_sep)))]
                col_h_b.append(linedata[4])
                col_m_b.append(linedata[5])
    print(f.name)

def plot():
    if offset.get() == 1:
        print('True')
        #some mathematical operations and graph plotting
    else:
        print('False')
        #other mathematical operations and graph plotting

def close_window():
    exit()

offset = IntVar()
checkbox = Checkbutton(root, text="offset subtraction", variable=offset)
checkbox.pack()

b_data = Button(root, text="Background", width=20, command=choose_b_file)
m_minus_b = Button(root, text="Plot", width=5, command=plot)
quit = Button(root, text="Quit", width=5, command=close_window)

b_data.pack()
m_minus_b.pack()
quit.pack()

root.mainloop()

Upvotes: 1

Related Questions